के लिए "UserId नहीं मिला" अपवाद देता है मैं JWT के साथ पहचान और भूमिका आधारित दावों के निर्माण के बारे में bitoftech tutorial के साथ अनुसरण कर रहा हूं। मेरा आवेदन उपयोगकर्ता int PK के साथ एक कस्टम उपयोगकर्ता तालिका है।CreateUserIdenityAsync कस्टम पहचान उपयोगकर्ता
वर्तमान में, GenerateUserIdentityAsync विधि बस एक अजीब UserId not found
त्रुटि देता है।
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
और User
इकाई में कार्यान्वयन: यहाँ मेरी कोड है अजीब तरह से पर्याप्त,,
public class AppUserManager : UserManager<User, int>
जब मैं डिबग उदाहरण this
:
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 वर्ग इतना की तरह परिभाषित किया गया है GenerateIdentityAsync
में UserId
संपत्ति है, लेकिन बेस में केवल id
है और मुझे आश्चर्य है कि यह कहां है ई यह त्रुटि हो रही है? (यह सही नहीं लगता है)
मैं 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 है नहीं सब (मेरे लिए)
मुझे कैसे पता चलेगा कि क्यों/जहां उपयोगकर्ता पहचान उपलब्ध नहीं है?
मोड विवरण:
मेरे 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
आप को पता चला के रूप में
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; }
}
यदि आपके पास सही कॉलम नाम हैं (आईडी के बजाय UserId या इसके विपरीत) आपके एस्पनेट डीबी में सत्यापित करें। कॉलम टाइप मिस्चैच की एक संभावना भी है (अन्वेषणकर्ताओं के रूप में nvarchar (256))। – Batuta