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

时间:2015-09-06 06:42:44

标签: c# asp.net-mvc asp.net-mvc-4

你好,我有点迷失在这里,我不知道该修复什么:

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

我一直在关注一个糟糕的教程,这是我到目前为止所要做的: (我想至少在我转向另一个之前先做到这一点)

以下是我的数据库表:

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

登录表

enter image description here

角色表

enter image description here

我使用inner join,我会有类似的东西

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

enter image description here

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

我有一个继承自roleprovider

的班级roleprovider

我只实现了两种方法,即:GetRolesForUserIsUserInRole

GetRolesForUser

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;
    }

的isUserInRole

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

的Web.Config

<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();
        }
    }

2 个答案:

答案 0 :(得分:2)

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

您的web.config应该类似于:

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

即。您需要将自定义roleprovider声明为默认提供程序。

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

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

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

如果这不起作用,请尝试在GetRolesForUser方法中添加断点。

答案 1 :(得分:0)

通过修复我的Web.Config解决了这个问题(然后一切都神奇地工作了)。它没有工作,因为搞砸了命名空间。 Joe为这个想法提供了完整的学分。

相关问题