Я следую по адресу bitoftech tutorial о создании утверждений с идентификатором и ролью с JWT. Пользователь моего приложения является пользовательской таблицей пользователя с int PK.CreateUserIdenityAsync возвращает исключение «UserId not found» для пользовательского IdentityUser
В настоящее время метод GenerateUserIdentityAsync просто возвращает странную ошибку UserId not found
. вот мой код:
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
и реализация в User
лица:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, int> manager, string authenticationType)
{
//error on this line: CreateIdentityAsync throws error
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
return userIdentity;
}
Мой класс UserManager определяется следующим образом:
public class AppUserManager : UserManager<User, int>
достаточно Жутко, когда я отладки, экземпляр this
в GenerateIdentityAsync
имеет свойство UserId
, но база имеет только id
, и мне интересно, если это так e это ошибка? (это звучит не так)
Я смотрел на source code (строка 80), но я не могу понять, где выбрасывается исключение.
Точное исключение бросают это:
UserId not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:
System.InvalidOperationException:
UserId not found.
И stack trace не все, что полезно (для меня)
Как узнать, почему/где UserId не доступен?
детали Режим:
Мои GrantResourceOwnerCredentials()
:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] {"*"});
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
User user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null) // this is NOT null
{
context.SetError("invalid_grant", "The username or password is incorrect");
return;
}
// this line fails
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
var ticket = new AuthenticationTicket(oAuthIdentity, null);
context.Validated(ticket);
}
И ApplicationUser
(который, в моем случае, это просто User
)
public partial class User : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public int UserId { get; set; }
public string Fullname { get; set; }
public string Address { get; set; }
public string ContactNumber { get; set; }
}
Проверьте в своем aspnet db, если у вас есть правильные имена столбцов (UserId вместо Id или наоборот). Также одна из возможностей - несоответствие типа столбца (uniqueidentifiers по сравнению с nvarchar (256)). – Batuta