0

My azure web app is authenticated by Microsoft Account login. I enabled it through Azure portal with changing authentication/authorization settings of web app.This does not need any change in web app code. And authentication part is working fine.

Now I want the email Id of the user who gets authenticated before accessing the web app.

I tried to read User.Identity as System.Security.Claims.ClaimsPrincipal. But this is coming as null.

How to access the email Id of authenticated user?

Joey Cai
  • 18,968
  • 1
  • 20
  • 30

2 Answers2

1

Go to your webapp and click Authentication / Authorization.Under Authentication providers, click on Microsoft Account. Click on wl.basic & wl.emails to enable them.

enter image description here

Then go to ExternalLoginCallback and add the following code:

var identity = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);
var emailClaim = identity.Identity.FindFirst(ClaimTypes.Email);
var email = emailClaim.Value;

The code result: enter image description here

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
1

I am now able to get authenticated user email, even though my asp.net mvc app was initially "No Authentication" app. I enabled MS authentication from the Azure portal for my web app. Now I fetch user email from System.Security.Claims.ClaimsPrincipal.Current.Claims object. It is automatically set by MS authentication flow.