Asp.net mvc 4 如何在MVC4中登录我的自定义用户表?
我想在MVC4代码优先应用程序中使用我的自定义用户表。我在数据库上下文中定义了advanceUser表:Asp.net mvc 4 如何在MVC4中登录我的自定义用户表?,asp.net-mvc-4,asp.net-membership,security,user-profile,Asp.net Mvc 4,Asp.net Membership,Security,User Profile,我想在MVC4代码优先应用程序中使用我的自定义用户表。我在数据库上下文中定义了advanceUser表: public class MyDatabase : DbContext { public DbSet<User> UserSet { get; set; } public DbSet<News> NewsSet { get; set; } ... } 当应用程序启动时,它调用: WebSecurity.Ini
public class MyDatabase : DbContext
{
public DbSet<User> UserSet { get; set; }
public DbSet<News> NewsSet { get; set; }
...
}
当应用程序启动时,它调用:
WebSecurity.InitializeDatabaseConnection("MyDatabase", "Users", "Id", "UserName", autoCreateTables: true);
在控制器中,我使用添加(实体)保存用户实体。保存后,我想登录用户。但它不起作用:
[HttpPost]
public ActionResult Register(User user)
{
var result = MyService.SaveUser(user);
WebSecurity.Login(result.UserName, result.Password, true);
return RedirectToAction("Index", "Profile", new { id = result.Id });
}
保存用户后,它的数据存储在我的数据库中,但它无法登录。我该怎么办
编辑:
使用我的业务方法保存用户实体是否正确?否则,我只能和你一起做
WebSecurity.CreateUserAndAccount()
如果我可以使用自己的保存方法,如何在数据库中保存密码?您可以直接使用表单身份验证
[HttpPost]
public ActionResult Register(User user)
{
var result = MyService.SaveUser(user);
SignIn(result.Id, "");
return RedirectToAction("Index", "Profile", new { id = result.Id });
}
public void SignIn(string accountId, string roles)
{
var authTicket = new FormsAuthenticationTicket(
1,
accountId,
DateTime.Now,
DateTime.Now.AddMinutes(20),
false,
roles
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
HttpContext.Current.Response.Cookies.Add(authCookie);
}
下面是一个用户类,它将帮助您解决密码问题。它依赖于BCrypt
public class UserAccount
{
public string Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password
{
get { return PasswordHash; }
set { PasswordHash = HashPassword(value); }
}
public string PasswordHash { get; private set; }
public List<string> Roles { get; set; }
public string AuthenticationRoles
{
get { return Roles == null ? "" : String.Join(",", Roles.Select(x => x.ToString())); }
}
public bool IsActive { get; set; }
public string Name { get; set; }
public bool PasswordIsValid(string password)
{
bool matches = BCrypt.Net.BCrypt.Verify(password, Password);
return matches;
}
private string HashPassword(string value)
{
return BCrypt.Net.BCrypt.HashPassword(value);
}
}
公共类用户帐户
{
公共字符串Id{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码
{
获取{return PasswordHash;}
设置{PasswordHash=HashPassword(value);}
}
公共字符串密码哈希{get;private set;}
公共列表角色{get;set;}
公共字符串身份验证角色
{
获取{returnroles==null?”:String.Join(“,”,Roles.Select(x=>x.ToString());}
}
公共bool IsActive{get;set;}
公共字符串名称{get;set;}
公共bool密码有效(字符串密码)
{
bool matches=BCrypt.Net.BCrypt.Verify(密码、密码);
返回比赛;
}
私有字符串哈希密码(字符串值)
{
返回BCrypt.Net.BCrypt.HashPassword(值);
}
}
您可以直接使用表单身份验证
[HttpPost]
public ActionResult Register(User user)
{
var result = MyService.SaveUser(user);
SignIn(result.Id, "");
return RedirectToAction("Index", "Profile", new { id = result.Id });
}
public void SignIn(string accountId, string roles)
{
var authTicket = new FormsAuthenticationTicket(
1,
accountId,
DateTime.Now,
DateTime.Now.AddMinutes(20),
false,
roles
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
HttpContext.Current.Response.Cookies.Add(authCookie);
}
下面是一个用户类,它将帮助您解决密码问题。它依赖于BCrypt
public class UserAccount
{
public string Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password
{
get { return PasswordHash; }
set { PasswordHash = HashPassword(value); }
}
public string PasswordHash { get; private set; }
public List<string> Roles { get; set; }
public string AuthenticationRoles
{
get { return Roles == null ? "" : String.Join(",", Roles.Select(x => x.ToString())); }
}
public bool IsActive { get; set; }
public string Name { get; set; }
public bool PasswordIsValid(string password)
{
bool matches = BCrypt.Net.BCrypt.Verify(password, Password);
return matches;
}
private string HashPassword(string value)
{
return BCrypt.Net.BCrypt.HashPassword(value);
}
}
公共类用户帐户
{
公共字符串Id{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码
{
获取{return PasswordHash;}
设置{PasswordHash=HashPassword(value);}
}
公共字符串密码哈希{get;private set;}
公共列表角色{get;set;}
公共字符串身份验证角色
{
获取{returnroles==null?”:String.Join(“,”,Roles.Select(x=>x.ToString());}
}
公共bool IsActive{get;set;}
公共字符串名称{get;set;}
公共bool密码有效(字符串密码)
{
bool matches=BCrypt.Net.BCrypt.Verify(密码、密码);
返回比赛;
}
私有字符串哈希密码(字符串值)
{
返回BCrypt.Net.BCrypt.HashPassword(值);
}
}
不要这样做。永远不要将密码以纯文本形式存储在数据库中。用盐腌的土豆条。这实际上是很难做到的-使用内置的会员系统代替。如果我将密码作为哈希代码存储在数据库中,问题会解决吗?看看这篇关于安全性的文章:并检查一下。不要这样做。永远不要将密码以纯文本形式存储在数据库中。用盐腌的土豆条。这实际上是很难做到的-使用内置的会员系统代替。如果我将密码作为哈希代码存储在数据库中,问题会解决吗?看看这篇关于安全性的文章:并检查。我第一次使用FormsAuthentication。它对我有效,但在使用FormsAuthentication登录后如何获取当前用户的数据?id、角色等.HttpContext.Current.User.Identity.Name-这将读取cookie,然后返回名称。我们正在表单身份验证票证中为用户Id设置名称。从那里,您可以查询数据库以获取其他用户数据。我第一次使用FormsAuthentication。它对我有效,但在使用FormsAuthentication登录后如何获取当前用户的数据?id、角色等.HttpContext.Current.User.Identity.Name-这将读取cookie,然后返回名称。我们正在表单身份验证票证中为用户Id设置名称。从那里,您可以查询数据库以获取其他用户数据