“类型 'Users' 上的 属性 'Claims' 不是导航 属性。”尝试使用 Owin 登录时
'The property 'Claims' on type 'Users' is not a navigation property.' when trying to login using Owin
当我尝试使用用户名(电子邮件)和密码登录时出现以下错误。
The property 'Claims' on type 'Users' is not a navigation property. The Reference and Collection methods can only be used with navigation properties. Use the Property or ComplexProperty method.
但是我不明白这个错误是从哪里来的,因为我能够成功地注册一个人,而无需在我自己的数据库中进行任何远程声明。
我尝试执行 userManager.FindAsync 时代码失败了。
try
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
Users user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType);
var cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType);
var properties = CreateProperties(user.Email);
var ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
catch(Exception ex)
{
throw new ArgumentException(string.Format("Could not find client data due to following error: {0}", ex));
}
我的用户 Class(从我的 sql 数据库创建):
public partial class Users
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }
public override string Email { get; set; }
public string Password { get; set; }
public override string UserName { get; set; }
}
单独的文件:
public partial class Users : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Users> manager, string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}
我在哪里忽略了可能导致我收到错误的东西?
注册正常:
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try {
var email = model.Email;
var password = model.Password;
var hashedPassword = PasswordHash.HashPassword(password);
Users user = new Users
{
UserName = email,
Email = email,
PasswordHash = hashedPassword,
Password = hashedPassword
};
IdentityResult result = await UserManager.CreateAsync(user, hashedPassword);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
catch(Exception ex)
{
throw new ArgumentException(string.Format("Error while trying to Register: ", ex));
}
我从来没有根据我在 http://bitoftech.net/ 上找到的演示让它工作,所以我使用演示代码作为基础,逐步迁移所有内容。现在我的身份验证有效了。
我对未来开发者的建议。立即使用演示作为基础。它就像一个魅力。
我确实注意到演示确实设置了声明并创建了声明 table。
当我尝试使用用户名(电子邮件)和密码登录时出现以下错误。
The property 'Claims' on type 'Users' is not a navigation property. The Reference and Collection methods can only be used with navigation properties. Use the Property or ComplexProperty method.
但是我不明白这个错误是从哪里来的,因为我能够成功地注册一个人,而无需在我自己的数据库中进行任何远程声明。
我尝试执行 userManager.FindAsync 时代码失败了。
try
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
Users user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType);
var cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType);
var properties = CreateProperties(user.Email);
var ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
catch(Exception ex)
{
throw new ArgumentException(string.Format("Could not find client data due to following error: {0}", ex));
}
我的用户 Class(从我的 sql 数据库创建):
public partial class Users
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }
public override string Email { get; set; }
public string Password { get; set; }
public override string UserName { get; set; }
}
单独的文件:
public partial class Users : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Users> manager, string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}
我在哪里忽略了可能导致我收到错误的东西?
注册正常:
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try {
var email = model.Email;
var password = model.Password;
var hashedPassword = PasswordHash.HashPassword(password);
Users user = new Users
{
UserName = email,
Email = email,
PasswordHash = hashedPassword,
Password = hashedPassword
};
IdentityResult result = await UserManager.CreateAsync(user, hashedPassword);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
catch(Exception ex)
{
throw new ArgumentException(string.Format("Error while trying to Register: ", ex));
}
我从来没有根据我在 http://bitoftech.net/ 上找到的演示让它工作,所以我使用演示代码作为基础,逐步迁移所有内容。现在我的身份验证有效了。
我对未来开发者的建议。立即使用演示作为基础。它就像一个魅力。
我确实注意到演示确实设置了声明并创建了声明 table。