Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

[office365 Exchange online][MVC5][EWS Managed Api] Need a hack to get access token ?

$
0
0

Hi there, I am using following code example to get access token from Azure AD online. I need to bridge EWS managed api with office 365 Api via Azure AD. The below code is working just fine in my MVC application but I am looking for a way to get the access token in simple string returning  function so that I can use it to make call against EWS mananged api.

private static string tempToken = "";

        public static string GetAccessToken()
        {
            return tempToken;
        }



        internal static async Task<OutlookServicesClient> EnsureOutlookServicesClientCreatedAsync(string capabilityName)
        {
            var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
            AuthenticationContext authContext = new AuthenticationContext(Settings.Authority, new NaiveSessionCache(signInUserId));
            try
            {
                DiscoveryClient discClient = new DiscoveryClient(Settings.DiscoveryServiceEndpointUri,
                async () =>
                {
                    var authResult = await authContext.AcquireTokenSilentAsync(Settings.DiscoveryServiceResourceId,
                    new ClientCredential(Settings.ClientId,
                    Settings.AppKey),
                    new UserIdentifier(userObjectId,
                    UserIdentifierType.UniqueId));
                    return authResult.AccessToken;
                });
                var dcr = await discClient.DiscoverCapabilityAsync(capabilityName);
                return new OutlookServicesClient(dcr.ServiceEndpointUri,
                async () =>
                {
                    var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId,
                    new ClientCredential(Settings.ClientId,
                    Settings.AppKey),
                    new UserIdentifier(userObjectId,
                    UserIdentifierType.UniqueId));
                    return authResult.AccessToken;
                });
            }
            catch (AdalException exception)
            {
                //Handle token acquisition failure
                if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently)
                {
                    authContext.TokenCache.Clear();
                }
                return null;
            }
        }

This is an excerpt from Microsoft single tenant application in MVC5.  In the code, I have created a function called GetAccessToken() which does nothing.. I am hoping you experts can help me figure out how to transfer the accessToken from EnsureOutlookServcesClientCreated function which is actually returning token, to  my GetAccessToken() so that I can make call against EWS managed Api. Sorry, If I sound pretty stupid but I really need your help in this regard. 

best regards,


Viewing all articles
Browse latest Browse all 7132

Trending Articles