我正在使用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