JWT ile Kimlik ve rol tabanlı haklar oluşturma hakkında bitoftech tutorial boyunca devam ediyorum. Uygulama kullanıcım int PK ile özel bir Kullanıcı tablosu.CreateUserIdenityAsync, özel IdentityUser için "UserId found" özel durumu döndürüyor
Şu anda, GenerateUserIdentityAsync yöntemi bir garip UserId not found
hatasını döndürür.
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
ve User
varlıktaki uygulama: İşte benim kod
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;
}
Benim UserManager sınıfı şöyle tanımlanır:
public class AppUserManager : UserManager<User, int>
Tuhaftır ki ben hata ayıklarken, örneğini this
GenerateIdentityAsync
, UserId
özelliğinde yok, ancak yalnızca id
taban var ve acaba bu nerede? e hata mı yapıyor? (doğru gelmiyor)
source code (satır 80) 'a bakıyordum ancak istisnanın nereye atılacağını anlayamıyorum.
atılan kesin istisnadır:
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.
Ve stack trace değil tüm bu yararlı (bana)
KullanıcıNo mevcut olmadığı neden/öğrenebilirim nasıl?
Mod ayrıntıları:
Benim 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);
}
Ve öğrendim gibi
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; }
}
Doğru sütun adlarına sahipseniz (ad yerine UserId veya tam tersi) aspnet db'nizde doğrulayın. Ayrıca bir olasılık, sütun tipi uyuşmazlığıdır (nvarchar'a karşı tekil tanımlayıcılar (256)). – Batuta