Include associated class objects (Applicationuser and Userprofile) - Applicationusermanager and OWIN

Asked

Viewed 69 times

1

I have the following problem:

Within the method GrantResourceOwnerCredentials in class OAuthProvider : OAuthAuthorizationServerProvider i am filling in custom properties to be returned with the Token. However, the object of the associated class UserProfile always comes null after I use the FindAsync, as shown in the row below:

ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

I can include via Ager loading the associated object as follows:

await userManager.Users.Include("UserProfile").SingleOrDefaultAsync(u => u.UserName == context.UserName);
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

Only I think it is not correct, because I check the username in the bank before checking if it is correct (because of the password).

I wanted to carry this object after using the FindAsync, but I don’t know how. How can I do it using the user?

  • There’s nothing wrong with the Include. I don’t understand your question. You want to check before if the user exists to load your information?

  • So I check if it is correct within the context if(context.UserName == null) { context.SetError("invalid_grant", "UserName field is required."); return; }

  • But what’s wrong with using the line with the .SingleOrDefault straightforward?

  • Well, I thought it wasn’t good practice.

  • No, there is no problem. Just do the checks without unnecessarily exposing the data. Otherwise everything is in order.

  • Okay, I get it. Thank you!

  • You want me to answer, just so we can close the question?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.