Sto seguendo lo bitoftech tutorial sulla creazione di attestazioni basate su identità e ruolo con JWT. L'utente dell'applicazione è una tabella utente personalizzata con PK int.CreateUserIdenityAsync restituisce l'eccezione "UserId non trovato" per IdentityUser personalizzato
Attualmente, il metodo GenerateUserIdentityAsync restituisce solo uno strano errore UserId not found
. qui è il mio codice:
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
e l'attuazione in User
entità:
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;
}
La mia classe UserManager è definita in questo modo:
public class AppUserManager : UserManager<User, int>
Stranamente, quando il debug, l'istanza this
in GenerateIdentityAsync
ha una proprietà UserId
, ma la base ha solo un id
e mi chiedo se sia così e sta saltando fuori? (non suona bene)
Stavo guardando il source code (riga 80) ma non riesco a capire dove viene lanciata l'eccezione.
L'eccezione esatto essere gettato è:
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.
E stack trace non è tutto ciò che utile (per me)
Come faccio a sapere il motivo per cui/in cui l'ID utente non è disponibile?
particolari modalità:
mio 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);
}
E la ApplicationUser
(che, nel mio caso, è solo 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; }
}
Verificare in aspnet db se si dispone dei nomi di colonna corretti (ID utente anziché ID o viceversa). Inoltre, una possibilità è quella di mancata corrispondenza del tipo di colonna (identificatori univoci rispetto a nvarchar (256)). – Batuta