ASP MVC中的自定义角色提供程序 [英] Custom role provider in ASP MVC

查看:104
本文介绍了ASP MVC中的自定义角色提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有点迷失在这里,我不知道该怎么解决:

Hello i am kinda at lost here and i don't know what to fix:

问题在于,我的应用程序仍未检测到分配的角色. 就像我有类似的东西一样 [Authorize(Roles="user")].它不会检测到我的登录信息,也不允许我继续该视图.

The issue is that, my application still doesn't detect the roles assigned. Like if i have something like [Authorize(Roles="user")]. It won't detect my login and won't allow me to proceed on that view.

我一直在跟踪一个糟糕的教程,到目前为止,这是我必须要做的: (我想至少先进入其他位置)

I've been following a bad tutorial and here's what i have to so far: (i want to make it first at least before i move on to another one)

这是我的数据库表:

为简单起见,我尚未添加任何哈希值:

I didn't put any hash yet for simplicity:

登录表

角色表

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

I use inner join and i will have something like this

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

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

I use code first EF 4.0 and here are my code snippets:

我有一个从roleprovider继承的类roleprovider.

我只实现了2种方法,分别是:GetRolesForUserIsUserInRole

I only implemented 2 methods from it, namely: GetRolesForUser and IsUserInRole

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>

对于目前仍无法正确解释代码的情况,我深表歉意,因为我仍在学习中.到目前为止,我的主要议程是首先使代码生效,但由于我不确定自己所缺少的内容,我莫名其妙地迷了路.

I apologize if i can't explain the code properly as of now because i am still in the process of learning it. My primary agenda as of now i to make the code work first but i am somehow lost because i am not particularly sure of what i am missing.

问题回顾: -Application无法检测数据库中的角色,并且如果我尝试登录,也不会让我继续.

Recap of the issue: -Application won't detect the roles from the database and won't let me proceed if i try logging in.

这是我的登录代码(我已实现身份验证模式)

edit: here is my login code( i have authentication mode implemented )

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

推荐答案

查看您发布的代码的一些技巧.

Some tips from looking at the code you posted.

您的web.config应该类似于:

Your web.config should look something like:

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

即您需要将自定义角色提供程序声明为默认提供程序.

I.e. you need to declare your custom roleprovider as the default provider.

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

You shold remove the following from your GetRolesForUser method:

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

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

The purpose of this method is to get the roles for the username passed as an argument - it shouldn't be concerned with the current user.

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

If this doesn't work, try putting a breakpoint in your GetRolesForUser method.

这篇关于ASP MVC中的自定义角色提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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