Asp.net mvc 使用存储库登录MVC?
我试图用登录视图、控制器和存储库验证用户 操作导致控制器失效 存储库中的方法Asp.net mvc 使用存储库登录MVC?,asp.net-mvc,Asp.net Mvc,我试图用登录视图、控制器和存储库验证用户 操作导致控制器失效 存储库中的方法 在您的登录方法中,您只需返回字符串(用户名)……但您没有设置任何cookies,所以 因此,您可以在登录方法后编写代码: if(String.IsNullOrEmpty(objuserrepository.Login(user.Username,user.Password)) { FormsAuthentication.SetAuthCookie( this.TextBox
在您的登录方法中,您只需返回字符串(用户名)……但您没有设置任何cookies,所以 因此,您可以在登录方法后编写代码:
if(String.IsNullOrEmpty(objuserrepository.Login(user.Username,user.Password))
{
FormsAuthentication.SetAuthCookie(
this.TextBox_username.Text.Trim(), flase);
FormsAuthenticationTicket ticket1 =
new FormsAuthenticationTicket(
1, // version
this.TextBox_username.Text.Trim(), // get username from the form
DateTime.Now, // issue time is now
DateTime.Now.AddMinutes(10), // expires in 10 minutes
false, // cookie is not persistent
"HR" // role assignment is stored
// in userData
);
HttpCookie cookie1 = new HttpCookie(
FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket1) );
Response.Cookies.Add(cookie1);
}
现在你有什么问题?建议你考虑使用。在数据库中存储纯文本密码是一种糟糕的做法。这不是验证用户名和密码,而是重定向到另一个页面。我希望它验证用户名和密码,并更改登录状态
public string Login(string Username, string Password )
{
if (!DB.Users.Any(x => x.Username == Username && x.Password == Password))
{
return Username;
}
throw new UnauthorizedAccessException();
return Username;
}
if(String.IsNullOrEmpty(objuserrepository.Login(user.Username,user.Password))
{
FormsAuthentication.SetAuthCookie(
this.TextBox_username.Text.Trim(), flase);
FormsAuthenticationTicket ticket1 =
new FormsAuthenticationTicket(
1, // version
this.TextBox_username.Text.Trim(), // get username from the form
DateTime.Now, // issue time is now
DateTime.Now.AddMinutes(10), // expires in 10 minutes
false, // cookie is not persistent
"HR" // role assignment is stored
// in userData
);
HttpCookie cookie1 = new HttpCookie(
FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket1) );
Response.Cookies.Add(cookie1);
}