我正在运行 CustomAuthenticationMvc 的 ServiceStack 用例示例,但是当我尝试登录时,我在 asp mvc 登录页面中输入
用户:管理员 密码:123
但显示错误消息(无效的用户名或密码),因此在 CustomValidation 项目中,但在 ASP.NET 中,这些是用于访问 HelloService 的用户名和密码...那么 CustomAuthenticationMvc 中的用户名和密码是多少访问HelloService?
谢谢
最佳答案
如果您查看 AppHost.cs,您可以看到身份验证是如何执行的:
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
if (!Membership.ValidateUser(userName, password)) return false;
....
}
}
因此它使用成员(member)提供程序框架来验证用户身份。
默认情况下,它将使用 SimpleMembership (例如 http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx ),默认连接来自 web.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-CustomAuthenticationMvc-20121011213234;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
您会注意到数据库(aspnet-CustomAuthenticationMvc-20121011213234
) - 它实际上并不存在。您必须通过 Visual Studio 中的服务器资源管理器
创建它。
然后您必须在数据库中创建一个用户。一种快速的方法是输入以下内容:
if (!WebSecurity.UserExists("testuser"))
WebSecurity.CreateUserAndAccount("testuser", "secret");
在某处的种子方法中。为了简单起见,您可以将其放入 AccountController
的 Login
方法中。
或者,如果您不介意这些,您可以模仿 CustomAuthentication 项目中的代码。
即替换
if (!Membership.ValidateUser(userName, password)) return false;
与
if (!CheckInDB(userName, password)) return false;
...
private bool CheckInDB(string userName, string password)
{
if (userName != "admin" && userName != "user") return false;
return password == "123";
}
关于asp.net-mvc - ServiceStack CustomAuthenticationMvc 管理员密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19596373/