我正在使用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="Data Source=(local);Initial Catalog=SKG;Integrated Security=True;MultipleActiveResultSets=True"" 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);
...在我的控制?
謝謝。
因此,在一個單獨的主題......只是一個建議的話,清理你的控制器代碼,使其更具可讀性將真正幫助下一個人的道路。這是一個建議,拿一粒鹽(沒有雙關語)。這只是一個建議,可能會幫助你... https://gist.github.com/ChaseFlorell/5218298 – 2013-03-22 01:37:41
@Chase:這不是一個雙關語。 – Dai 2013-03-22 01:43:38
@Dai閱讀OP的代碼'model.Password,user.First()。Salt.ToString())'...好的好吧,它不是很清楚,我絕對不好笑... – 2013-03-22 01:47:18