根据登录用户的角色显示不同的页面 [英] display different page as per the role of the logged in user

查看:1342
本文介绍了根据登录用户的角色显示不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户成功登录后,我们如何根据自己的角色向用户显示页面。假设他的角色是admin,他将被呈现为欢迎管理员用户或普通用户欢迎!!!。

到目前为止我已经完成了登录页面,其中用户显示单个AfterLogin页面。但不知道如何做到这一点,向不同的访问级别用户显示不同的页面。



  public  class AppUserController:Controller 
{
public ActionResult Login()
{
return 视图();
}
[HttpPost]
[ValidateAntiForgeryToken]
<跨度类= 代码关键字>公共的ActionResult登录(APPUSER APPUSER)
{
if (ModelState.IsValid)
{
using (TestSystemEntities db =新TestSystemEntities())
{
变种v = db.AppUsers <跨度类= 代码关键字>其中(A => a.USERNAME.Equals(APPUSER。 USERNAME)&& a.PASSWORD.Equals(appUser.PASSWORD))。SingleOrDefault();
if (v!= null
{
Session [ LogedUserFullname] = v.USERNAME.ToString();
会话[ LoggedUserRole] = v.USERROLE.ToString();
return RedirectToAction( AfterLogin);
}
else ModelState.AddModelError( 提供的用户名或密码不正确。);
}
}
返回 查看(appUser);
}
public ActionResult AfterLogin()
{
// if(Session [ LogedUserID]!= null ){ return 查看(); }
// else { return RedirectToAction( 索引); }
return 查看();
}
}





谢谢!

解决方案

< blockquote>如果你只想写,如果他的角色是'admin',他将被呈现给欢迎管理员用户或正常用户欢迎!!!。然后你可以使用ViewBag并根据Controller中的Session保存文本。否则你也可以在视图中检查Session。



如果你想显示不同的视图,那么你可以根据角色创建不同的视图然后



  public  ActionResult AfterLogin()
{
< span class =code-keyword> if (会话[ LogedUserID] = = null
{ return 查看( 登录); }
if (会话[ LoggedUserRole ]。ToString()== admin
{ return 查看( AdminDashBoard); }
else if (会话[ LoggedUserRole]。ToString()== 员工
{ return 查看( EmployeeDashBoard); }

// else {return RedirectToAction(Index); }
return View();
}





有很多方法,无论你喜欢什么,都可以通过那个。


After the successful login of a user, how can we display pages to the user according to his role. Suppose if his role is "admin" he will be rendered to "Welcome Admin User" or if normal user "Welcome!!!".
So far I've done till the login page, where the user is displayed single "AfterLogin" page. But do not know how to do this, show different pages to different access level user.

public class AppUserController : Controller
    {
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(AppUser appUser)
        {
            if (ModelState.IsValid)
            {
                using (TestSystemEntities db = new TestSystemEntities())
                {
                    var v = db.AppUsers.Where(a => a.USERNAME.Equals(appUser.USERNAME) && a.PASSWORD.Equals(appUser.PASSWORD)).SingleOrDefault();
                    if (v != null)
                    {
                        Session["LogedUserFullname"] = v.USERNAME.ToString();
                        Session["LoggedUserRole"] = v.USERROLE.ToString();
                        return RedirectToAction("AfterLogin");
                    }
                    else ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            return View(appUser);
        }
        public ActionResult AfterLogin()
        {
            //if (Session["LogedUserID"] != null) { return View(); }
            //else { return RedirectToAction("Index"); }
            return View();
        }
    }



Thanks!

解决方案

If you want to just write "if his role is 'admin' he will be rendered to "Welcome Admin User" or if normal user "Welcome!!!"." then you can use ViewBag and save the text as per the Session in Controller. Otherwise you can check for Session in the view itself also.

If you want to show different view then you can create different view as per the roles and then

public ActionResult AfterLogin()       
{
    if (Session["LogedUserID"] == null) 
        { return View("Login"); }
    if (Session["LoggedUserRole"].ToString() == "admin")
        { return View("AdminDashBoard"); }
    else if (Session["LoggedUserRole"].ToString() == "employee")
        { return View("EmployeeDashBoard"); }

    //else { return RedirectToAction("Index"); }
    return View();
}



There are many ways, whichever you like, you can go through that one.


这篇关于根据登录用户的角色显示不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆