asp.net-mvc-4 – 表单身份验证:角色(MVC 4)C#
| 
                         我正在尝试在我的应用程序中实现表单身份验证.我举了各种例子,看了一下这个论坛和ASP.net MVC中提供的示例和问题,但我无法让它工作. 我设法验证我的用户,但角色似乎不起作用:-( 我已经设置了我的Web.Config如下: < authentication mode =“Forms”> 在我的控制器中,我将索引页面设置为AllowAnonymous,然后检查用户是否经过身份验证.如果没有,则重定向到登录页面.. [AllowAnonymous]
    public ActionResult Index(string sortOrder,string searchString,string currentFilter,int? page)
    {
        if (!Request.IsAuthenticated)
        {
            return RedirectToAction("Login","Account");
        }
//Find all the employees
        var employees = from s in db.Employees
                       select s;
//Pass employees to the view (All works fine)
return View(employees.ToPagedList(pageNumber,pageSize));
} 
 这一切都在100%工作 我的登录代码如下所示: [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(User user,string returnUrl)
    {
        var myUser = db.Users.Where(b => b.UserName == user.UserName).FirstOrDefault();
        if(myUser != null)
        {
            if(myUser.Password==user.Password)
            {
                //These session values are just for demo purpose to show the user details on master page
                //Session["User"] = user;
                ICollection<UserAccessLevel> levels = db.UserAccessLevels.Where(b => b.UserId == myUser.UserId).ToList();
                //Session["levels"] = levels;
                //Let us now set the authentication cookie so that we can use that later.
                FormsAuthentication.SetAuthCookie(user.UserName,false);
                return RedirectToAction("Index","Employee");
            }
        }
        ViewBag.Message = "Invalid User name or Password.";
        return View(user);
    } 
 我在Global.asax文件中也有以下代码: protected void FormsAuthentication_OnAuthenticate(Object sender,FormsAuthenticationEventArgs e)
    {
        if (FormsAuthentication.CookiesSupported == true)
        {
            if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                try
                {
                    //let us take out the username now                
                    string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                    string roles = string.Empty;
                    using (TrainingContext entities = new TrainingContext())
                    {
                        User user = entities.Users.SingleOrDefault(u => u.UserName == username);
                        roles = "admin";//user.Roles;
                    }
                    //Let us set the Pricipal with our user specific details
                    e.User = new System.Security.Principal.GenericPrincipal(
                       new System.Security.Principal.GenericIdentity(username,"Forms"),roles.Split(';'));
                }
                catch (Exception)
                {
                    //somehting went wrong
                }
            }
        }
    } 
 当我登录我的FormsAuthentication_OnAuthenticate执行并且一切看起来都很好.我的用户已设置,我在会话中的角色也在那里…… 但是,当我点击我的员工/索引屏幕的详细信息时,它会将我带回登录屏幕(我希望它能够将我带到我点击的员工的详细信息,因为我已登录并且我被设置为管理员角色) 请你协助我试着解决问题.我已经坐了18个多小时才试图解决这个问题. 我已经看过这些解决方案了,你可以看到我的大部分代码来自那里…… 如果您需要有关我的代码的更多详细信息,您也可以从GitHub下载它 非常感谢您的帮助. 解决方法如果您转到数据库并查找为用户分配角色的表(可能由SimpleMembership生成?),您的用户是否具有“管理员”角色?看起来您只是在FormsAuthentication_OnAuthenticate方法中分配角色而不在DB中实际设置它. // Your method (...) User user = entities.Users.SingleOrDefault(u => u.UserName == username); roles = "admin";//user.Roles; 而且,虽然我不完全确定,[Authorize(Roles =“admin”)]可能正在使用您的角色提供程序并检查用户是否在数据库中具有/没有该角色. (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
- asp.net-mvc – 值不能为空或为空.参数名称:contentPath
 - asp.net-mvc – 在ASP.NET MVC中的LinkButton
 - ASP.NET:将内容注入所有Response流
 - asp.net – 如何在C#2.0中的Web.config中加密用户名和密码
 - asp.net – Windows应用程序与Web应用程序开发
 - asp.net-mvc – 控制器操作无法从JSON读取Guid POST
 - asp.net-mvc-3 – Azure网站上的RavenDb – 访问被拒绝
 - Jquery+ajax请求data显示在GridView上(asp.net)
 - asp.net-mvc – 不应加载引用程序集以执行
 - asp.net-mvc – 神秘的ASP.NET MVC Action高延迟问题?
 
- asp.net-mvc – MVC“添加控制器”是“无法检索元
 - asp.net – 我应该使用WebMatrix构建一个真实世界
 - asp.net – MVC4 – ContextDependentView – 这
 - asp.net – VirtualPath位于当前应用程序根目录之
 - .NET 3.5 / VS 2008上的ASP.NET Web Services的自
 - asp.net-mvc – SelectListItem中的Selected属性
 - asp.net-mvc – asp.net mvc博客引擎
 - asp.net – Ajax Control Toolkit正在加载太多脚
 - asp.net-web-api – Web Api:找不到System.Net.
 - asp.net-mvc – 模型单元测试能否真正独立,如何[
 
