我正在使用远程数据库创建MVC5网站。我的一个表包含用户/密码,因此我正在创建一个新的Auth控制器,试图从该表中读取数据。
我正在关注这个很棒的教程:http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-2
但是当我尝试登录时,我总是得到一个例外。
[ArgumentNullException: Value cannot be null. Parameter name: value]
System.Security.Claims.Claim..ctor(String type, String value, String valueType, String issuer, String originalIssuer, ClaimsIdentity subject, String propertyKey, String propertyValue) +11059233
System.Security.Claims.Claim..ctor(String type, String value, String valueType) +29
Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +790
登录时出现问题:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(TUser model, string returnUrl)
{
if (ModelState.IsValid)
{
TUser user = await UserManager.FindAsync(model.login, model.pass);
if (user != null)
{
ClaimsIdentity identity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
错误出现在最后一行:
ClaimsIdentity identity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
我还尝试将SecurityStamp添加到用户,正如许多人所建议的那样:
user.SecurityStamp = Guid.NewGuid().ToString();
我的TUser课程来自Ado.Net。看起来像这样:
public partial class TUser : IdentityUser
{
public TUser()
{
this.TEventUsers = new HashSet<TEventUser>();
this.TGifts = new HashSet<TGift>();
this.TKits = new HashSet<TKit>();
this.SecurityStamp = Guid.NewGuid().ToString();
}
public int idUser { get; set; }
public string login { get; set; }
public string pass { get; set; }
public string name { get; set; }
public short userType { get; set; }
public System.DateTime insertDate { get; set; }
public virtual ICollection<TEventUser> TEventUsers { get; set; }
public virtual ICollection<TGift> TGifts { get; set; }
public virtual ICollection<TKit> TKits { get; set; }
public virtual TUserType TUserType { get; set; }
}