예제 #1
0
 public bool AzureCacheFileExists()
 {
     return(TokenCacheHelper.CacheFileExists());
 }
예제 #2
0
        public async Task <AuthenticateResponse> AzureSingleSignOn()
        {
            string clientId    = ConfigurationManager.AppSettings["aad:clientId"];
            string tenantId    = ConfigurationManager.AppSettings["aad:tenantId"];
            string aadInstance = ConfigurationManager.AppSettings["aad:aadInstance"];

            string[] scopes = ConfigurationManager.AppSettings["aad:scopes"].Split(new[] { ',' }); //"user.read"

            string authority = String.Format(CultureInfo.InvariantCulture, "{0}{1}", aadInstance, tenantId);

            app = PublicClientApplicationBuilder
                  .Create(clientId)
                  .WithDefaultRedirectUri()
                  .WithAuthority(authority)
                  .Build();
            //this enables the cache to be saved to a file in the clients install directory
            TokenCacheHelper.EnableSerialization(app.UserTokenCache);
            //AuthenticationResult authenticationResult = new AuthenticationResult("00", false, "00", DateTimeOffset.MinValue, DateTimeOffset.MinValue, "00", null, "", new[] { "" }, Guid.Empty);
            AuthenticateResponse response = null;
            AuthenticationResult authRes  = null;

            try
            {
                var accounts = await app.GetAccountsAsync();

                if (accounts.Any())
                {
                    account = accounts.FirstOrDefault();
                    authRes = await app.AcquireTokenSilent(scopes, account).ExecuteAsync();
                }
                else
                {
                    authRes = await app.AcquireTokenSilent(scopes, System.Security.Principal.WindowsIdentity.GetCurrent().Name).ExecuteAsync();
                }

                if (authRes.Account != null)
                {
                    response      = SetAuthenticationResponse(authRes);
                    response.Meta = new System.Collections.Generic.Dictionary <string, string>();
                    response.Meta.Add("token", authRes.IdToken);
                    response.Meta.Add("secret", authRes.AccessToken);
                    return(response);
                }
            }
            catch (MsalUiRequiredException ax)
            {
                try
                {
                    if (ax.ErrorCode == MsalError.FailedToAcquireTokenSilentlyFromBroker || ax.ErrorCode == MsalError.NoAccountForLoginHint || ax.ErrorCode == MsalError.CodeExpired)
                    {
                        authRes = await app.AcquireTokenInteractive(scopes).ExecuteAsync();

                        response = SetAuthenticationResponse(authRes);
                        return(response);
                    }
                    else
                    {
                    }
                }
                catch (MsalClientException icx)
                {
                    Logger.Info("Login:"******"Login:"******"Login:", ex);
                if (account != null)
                {
                    await app.RemoveAsync(account);
                }
            }
            return(null);
        }