Пример #1
0
        public static async Task <OAuth.AuthenticationResult> GetToken(string userUniqueId, OAuth.AuthenticationRequest request)
        {
            AuthenticationSettings authenticationSettings = AuthenticationSettings.GetFromAppSettings();

            OAuth.AuthenticationResult authenticationResult;

            var tokenCache = OAuth.TokenCacheFactory.GetTokenCache <ADALTokenCache>(request);

            Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authenticationSettings.EndpointUrl + "/" + authenticationSettings.Tenant, tokenCache);
            var result = await context.AcquireTokenSilentAsync(request.ResourceId, new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(authenticationSettings.ClientId, authenticationSettings.ClientSecret), new Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier(userUniqueId, Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType.UniqueId));

            authenticationResult = ConvertAuthenticationResult(result, tokenCache);

            return(authenticationResult);
        }
Пример #2
0
        public static async Task <OAuth.AuthenticationResult> GetTokenByAuthCodeAsync(string authorizationCode, OAuth.AuthenticationRequest request)
        {
            AuthenticationSettings authenticationSettings = AuthenticationSettings.GetFromAppSettings();

            OAuth.AuthenticationResult authenticationResult;

            var tokenCache = OAuth.TokenCacheFactory.GetTokenCache <ADALTokenCache>(request);

            Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authenticationSettings.EndpointUrl + "/" + authenticationSettings.Tenant, tokenCache);
            Uri redirectUri = new Uri(authenticationSettings.RedirectUrl);
            var result      = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, redirectUri, new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(authenticationSettings.ClientId, authenticationSettings.ClientSecret));

            authenticationResult = ConvertAuthenticationResult(result, tokenCache);

            return(authenticationResult);
        }