user1960169
user1960169

Reputation: 3653

How to get email id of logged user in Azure AD in ios

In my app I want to integrate Azure loging and get the email id and send to my server. I have done this so far and from this I can get the access token.

- (void)acquireTokenInteractive:(id)sender
 {
ADTestAppSettings* settings = [ADTestAppSettings settings];
NSString* authority = [settings authority];
NSString* resource = [settings resource];
NSString* clientId = [settings clientId];
NSURL* redirectUri = [settings redirectUri];
ADUserIdentifier* identifier = [self identifier];
ADCredentialsType credType = [self credType];

BOOL validateAuthority = _validateAuthority.selectedSegmentIndex == 0;

ADAuthenticationError* error = nil;
ADAuthenticationContext* context = [[ADAuthenticationContext alloc] initWithAuthority:authority
                                                                    validateAuthority:validateAuthority
                                                                                error:&error];
if (!context)
{
    NSString* resultText = [NSString stringWithFormat:@"Failed to create AuthenticationContext:\n%@", error];
    [_resultView setText:resultText];
    return;
}

[context setCredentialsType:credType];

if ([self embeddedWebView])
{
    [context setWebView:_webView];
    //[_authView setFrame:self.view.frame];

    [UIView animateWithDuration:0.5 animations:^{
        [_acquireSettingsView setHidden:YES];
        [_authView setHidden:NO];
    }];
}

__block BOOL fBlockHit = NO;

[context acquireTokenWithResource:resource
                         clientId:clientId
                      redirectUri:redirectUri
                   promptBehavior:[self promptBehavior]
                   userIdentifier:identifier
             extraQueryParameters:nil
                  completionBlock:^(ADAuthenticationResult *result)
{
    if (fBlockHit)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Error!"
                                                                           message:@"Completion block was hit multiple times!"
                                                                    preferredStyle:UIAlertControllerStyleAlert];

            [self presentViewController:alert animated:YES completion:nil];
        });

        return;
    }
    fBlockHit = YES;



    dispatch_async(dispatch_get_main_queue(), ^{
        [self updateResultView:result];

        [_webView loadHTMLString:@"<html><head></head><body>done!</body></html>" baseURL:nil];
        [_authView setHidden:YES];
        [self.view setNeedsDisplay];

        [[NSNotificationCenter defaultCenter] postNotificationName:ADTestAppCacheChangeNotification object:self];
    });
}];

}

But how can I retrieve the email id of logged user. Please help me. Thanks

Upvotes: 0

Views: 909

Answers (1)

Nan Yu
Nan Yu

Reputation: 27538

What is the resource you want to access via the access token ? If resource is https://graph.windows.net/,you could use Azure AD Graph API to get the email information of the signed-in user :

GET https://graph.windows.net/me?api-version=1.6
Authorization: Bearer yourAccessToken

If resource is https://graph.microsoft.com/,you could use Microsoft Graph API to get the email information of the signed-in user:

Get https://graph.microsoft.com/v1.0/me
Authorization: Bearer yourAccessToken

In response you could check UserPrincipalName claim value which is an email address that can receive emails

Upvotes: 1

Related Questions