繁体   English   中英

ASP MVC 中的自定义角色提供程序

[英]Custom role provider in ASP MVC

你好,我有点迷失在这里,我不知道要解决什么问题:

问题是,我的应用程序仍然没有检测到分配的角色 就像我有类似[Authorize(Roles="user")] 它不会检测到我的登录信息,也不会允许我继续查看该视图。

我一直在学习一个糟糕的教程,这是我到目前为止必须做的:(我想至少在我转向另一个之前先完成它)

这是我的数据库表:

为简单起见,我还没有放置任何哈希:

登录表

在此处输入图片说明

角色表

在此处输入图片说明

我使用inner join ,我会有这样的东西

select A.username, A.role from login A INNER JOIN roles B on A.role = B.id

在此处输入图片说明

我首先使用代码 EF 4.0,这里是我的代码片段:

我有一个类roleprovider从继承roleprovider

我只从中实现了 2 个方法,即: GetRolesForUserIsUserInRole

获取用户角色

public override string[] GetRolesForUser(string uname)
    {
        if (!HttpContext.Current.User.Identity.IsAuthenticated)
        {
            return null;
        }

        var cacheKey = string.Format("{0_role}", uname);
        if (HttpRuntime.Cache[cacheKey] != null)
        {
            return (string[])HttpRuntime.Cache[cacheKey];
        }

        string[] roles = new string[] { };
        using (EmployeeContext emp = new EmployeeContext())
        {
            roles = (from a in emp.login
                     join b in emp.roles on a.role equals b.id
                     where a.username.Equals(uname)
                     select b.role).ToArray<string>();
            if (roles.Count() > 0)
            {
                HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration);
            }
        }
        return roles;
    }

是用户角色

public override bool IsUserInRole(string uname, string roleName)
{
     var userRoles = GetRolesForUser(uname);
     return userRoles.Contains(roleName);
}

网页配置

<roleManager>
  <providers>
    <clear/>
    <add name="roleprovider"  type="MvcApplication6.Helper.roleprovider"/>
  </providers>
</roleManager>

如果我现在无法正确解释代码,我深表歉意,因为我仍在学习它的过程中。 到目前为止,我的主要议程是首先让代码工作,但我不知何故迷失了,因为我不太确定我错过了什么。

问题回顾: - 应用程序不会从数据库中检测角色,如果我尝试登录,也不会让我继续。

编辑:这是我的登录代码(我实现了身份验证模式)

   [HttpGet]
    [ActionName("login")]
    public ActionResult login_load()
    {
        return View();
    }

    [HttpPost]
    [ActionName("login")]

    public ActionResult login_post(string uname,string pword)
    {
        using (EmployeeContext emp = new EmployeeContext())
        {

            int success = emp.login.Where(x => x.username == uname && x.password == pword).Count();
            if (success == 1)
            {
                FormsAuthentication.SetAuthCookie(uname, false);

                return RedirectToAction("Details", "Enrollment");
            }
            return View();
        }
    }

查看您发布的代码的一些提示。

您的 web.config 应该类似于:

<roleManager enabled="true" defaultProvider="roleprovider">
  <providers>
    <clear/>
    <add name="roleprovider"  type="MvcApplication6.Helper.roleprovider"/>
  </providers>
</roleManager>

即您需要将您的自定义角色提供者声明为默认提供者。

您应该从GetRolesForUser方法中删除以下内容:

if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
    return null;
}

此方法的目的是获取作为参数传递的用户名的角色 - 它不应该与当前用户有关。

如果这不起作用,请尝试在GetRolesForUser方法中放置一个断点。

通过修复我的 Web.Config 解决了这个问题(然后一切都神奇地工作了)。 它不工作,因为搞砸了命名空间。 完全归功于乔的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM