2013-03-22 30 views
0

我正在使用ASP.net MVC。 所以我想在渴望的時候設置用戶角色。如何在MVC中設置用戶角色?

所以這是我的控制器

public ActionResult LogOn(LogOnModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    {     
     var user = GetuserByname(model.UserName); 
     if (user.ToList().Count == 1) 
     { 
      string dbPassword = user.First().UserPassword.ToString(); 
      string enterPassword = CreatePasswordHash(model.Password, user.First().Salt.ToString()); 

      if (dbPassword.ToString().Trim() == enterPassword.ToString().Trim()) 
      { 
       FormsAuthentication.SetAuthCookie(user.First().tblUserRole.RoleName, model.RememberMe); 
       Session["logged"] = user.First().Username; 


       string roleName = user.First().tblUserRole.RoleName; 
       Roles.AddUserToRole(model.UserName, roleName); 
       return RedirectToAction("Index", "Home"); 
      } 
     }     
    }    
    return View(model); 
} 

這是我的webconfig文件

<?xml version="1.0" encoding="utf-8"?> 

<configuration> 

    <connectionStrings> 
     <add name="ApplicationServices1" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> 
     <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> 
     <add name="SKGEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(local);Initial Catalog=SKG;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 

    <authentication mode="Forms"> 
     <forms loginUrl="~/Login/LogOn" timeout="2880" /> 
    </authentication> 

    <membership> 
     <providers> 
      <clear /> 
      <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="3" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> 
     </providers> 
    </membership> 

    <profile> 
     <providers> 
      <clear /> 
      <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" /> 
     </providers> 
    </profile> 

    <roleManager enabled="true"> 
     <providers> 
      <clear /> 
      <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> 
      <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> 
     </providers> 
    </roleManager> 

</configuration> 

但是有錯誤,它說...

與網絡相關的或實例在建立與SQL Server的連接時發生特定錯誤

...我不知道如何解決它。

而且我怎麼能設置...

Roles.AddUserToRole(xxx, yyy); 

...在我的控制?

謝謝。

+0

因此,在一個單獨的主題......只是一個建議的話,清理你的控制器代碼,使其更具可讀性將真正幫助下一個人的道路。這是一個建議,拿一粒鹽(沒有雙關語)。這只是一個建議,可能會幫助你... https://gist.github.com/ChaseFlorell/5218298 – 2013-03-22 01:37:41

+1

@Chase:這不是一個雙關語。 – Dai 2013-03-22 01:43:38

+1

@Dai閱讀OP的代碼'model.Password,user.First()。Salt.ToString())'...好的好吧,它不是很清楚,我絕對不好笑... – 2013-03-22 01:47:18

回答

3

錯誤消息說您的數據庫是SQL服務器,無法訪問。

你的供應商(角色,會員的個人資料)被設置爲使用ApplicationServices連接字符串,它是:

data source=.\SQLEXPRESS; 
Integrated Security=SSPI; 
AttachDBFilename=|DataDirectory|aspnetdb.mdf; 
User Instance=true 

...但是你的其他的連接字符串

Data Source=(local); 
Initial Catalog=SKG; 
Integrated Security=True; 
MultipleActiveResultSets=True 

。 ..這是完全不同的數據庫服務器。

更改您的提供者配置以使用正確的數據庫服務器。

+0

問題格式化讓我發瘋,所以我更專注於修復它,而不是專注於回答這個問題。 +1來解決問題的根源,不幸的是,編碼風格將使OP在許多相同類型的問題上留下來。 – 2013-03-22 01:10:06

+0

@ChaseFlorell「web.config」有什麼問題?只是它的格式? – Yuck 2013-03-22 01:28:12

+0

只是有很多不相關的信息。在SO上發佈之前,從代碼中去除不相關的信息是一種很好的做法。 – 2013-03-22 01:31:44