我正在使用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);
}
这是我的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);
...在我的控制器中?
谢谢。
答案 0 :(得分: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
...完全是一个不同的数据库服务器。
更改提供程序配置以使用正确的数据库服务器。