2015-09-06 243 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解決了這個問題(然後所有東西都神奇地工作)。這不起作用,因爲命名空間混亂了。對這個想法完全信任喬。

相關問題