aws / aws-aspnet-cognito-identity-provider

ASP.NET Core Identity Provider for Amazon Cognito
https://aws.amazon.com/developer/language/net/
Apache License 2.0
215 stars 89 forks source link

How do i get current user from home controller? #79

Closed josephd9 closed 5 years ago

josephd9 commented 5 years ago

Title says it all. I can't figure out how to get the current user from my home controller.

I have a 2nd question, I can create a new post for it, but I figured it'd try it here. Is there any existing framework for a user deleting their account? Or do I have to write it myself with UserManager.DeleteAsync(); Just asking so I dont waste anymore time looking for it.

Thanks!

jlwhitfill commented 5 years ago

If your user is authenticated, you can call this. I use this in any controller I need that info, where User is the current claims principal:

var user = await _userManager.GetUserAsync(User).ConfigureAwait(false);

assyadh commented 5 years ago

You can inject a UserManager to your controller. Example: https://github.com/aws/aws-aspnet-cognito-identity-provider/blob/master/samples/Samples/Areas/Identity/Pages/Account/Register.cshtml.cs#L22

Than call _userManager.GetUserAsync(User) as mentioned by @jlwhitfill .

You actually don't need to be authenticated, however the user you retrieve won't be authenticated either.

Deleting a user is done by calling UserManager.DeleteAsync(). There is no override in the CognitoUserManager class because the base implementation is already working with Cognito

josephd9 commented 5 years ago

How do i get the current claims principal? I tried var user = ClaimsPrincipal.Current; and it's always null.

josephd9 commented 5 years ago

I found it, sorry for the bad question. In case anyone else looking for it comes here, I found it here: var user = _signInManager.Context.User;