我正在使用 ASP.net 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);
}
这是我的网络配置文件
<?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);
...在我的控制器中?
谢谢你。