Sivamohan Reddy
Sivamohan Reddy

Reputation: 574

Azure AD How to get the Tenant Id from the user when he authenticated to our application

I've created an Asp.net application with Azure Active Directory authentication by using OAuth2 and working as expected.

Now when the user login to my application, I'm getting the user data like User Email id and provider key

Now the requirement is, I need to get user tenant Id from the response.

Is there any chance to get the user tenant id from the response?

Here I'm using GetExternalLoginInfoAsync() to get the authenticated user data.

  private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }
   var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

Appreciate your help.

Upvotes: 3

Views: 3409

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

Now the requirement is, I need to get user tenant Id from the response.

loginInfo.Login.LoginProvider would look like as follows:

https://sts.windows.net/{tenant-Id}/

Or you could retrieve the tenant Id via the following code snippet:

var tenantId = loginInfo.ExternalIdentity.FindFirst(c => c.Type == "http://schemas.microsoft.com/identity/claims/tenantid").Value;

and make sure that your application should have the following permissions: 1) Read directory data 2) Read all users full profiles 3) Sign in and read user profile

Upvotes: 3

Related Questions