2015-09-06 202 views
2

你好,我是有点在这里失去了,我不知道要解决什么:自定义角色提供

的问题是,我的应用程序仍然没有检测到角色分配。 就像我有类似 [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

我只执行2种方法从中,即: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(); 
     } 
    } 
+0

“应用程序不会检测到数据库中的角色”有点含糊。究竟会发生什么? –

+0

即使我拥有[Authorize(Roles =“users”)],我的当前登录角色是'users',它也不会让我继续。让我编辑我的帖子。 –

回答

2

一些技巧从看着你发布的代码。

你的web.config应该是这个样子:

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

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

您shold删除您GetRolesForUser方法如下:

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

这种方法的目的是为了获得作为参数传递的用户名的角色 - 它不应该与当前用户关注。

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

+0

我曾尝试过您的网络配置,但我得到了像'解析器错误'之类的东西。我必须用这个来避免它。 –

+0

这里的链接:http://stackoverflow.com/questions/32420291/parser-error-message-could-not-load-type-from-assembly/32420477?noredirect=1#comment52706815_32420477 –

+0

@Carlo - 该错误消息说它无法找到您的自定义roleprovider类。从现在开始,这是向前迈出的一步,因为它至少试图加载它。您需要检查提供者类的名称(包括名称空间)以及包含它的程序集的名称是否与web.config中的名称相匹配。 – Joe

0

通过修复我的Web.Config解决了这个问题(然后所有东西都神奇地工作)。这不起作用,因为命名空间混乱了。对这个想法完全信任乔。

相关问题