Ejemplo n.º 1
0
        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var mailId = Convert.ToString(LocalStorage.GetSetting("mailId"));
            var name   = Convert.ToString(LocalStorage.GetSetting("name"));

            if (!String.IsNullOrEmpty(mailId))
            {
                authResult = await authContext.AcquireTokenSilentAsync("https://login.windows.net/appsthepagedot.onmicrosoft.com", clientId);

                if (authResult.Status == Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationStatus.ClientError)
                {
                    var refreshToken = Convert.ToString(LocalStorage.GetSetting("refreshToken"));
                    if (!String.IsNullOrEmpty(refreshToken))
                    {
                        authResult = await authContext.AcquireTokenByRefreshTokenAsync(refreshToken, clientId);

                        SaveTokenLocal();
                    }
                    else
                    {
                        authContext.AcquireTokenAndContinue("https://graph.windows.net/", clientId, new Uri("http://www.google.de"), authenticationContextDelegate);
                    }
                }

                loginButton.Visibility  = Visibility.Collapsed;
                logoutButton.Visibility = Visibility.Visible;
                userName.Text           = "Hallo, " + name;
                var accessToken = LocalStorage.GetSetting("accessToken");
            }
            else
            {
                logoutButton.Visibility = Visibility.Collapsed;
                loginButton.Visibility  = Visibility.Visible;
            }
        }
        public static async Task <AuthResult> GetToken(string userUniqueId, Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache tokenCache, string resourceId)
        {
            Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(AuthSettings.EndpointUrl + "/" + AuthSettings.Tenant, tokenCache);
            var result = await context.AcquireTokenSilentAsync(resourceId, new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(AuthSettings.ClientId, AuthSettings.ClientSecret), new Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier(userUniqueId, Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType.UniqueId));

            AuthResult authResult = AuthResult.FromADALAuthenticationResult(result, tokenCache);

            return(authResult);
        }
Ejemplo n.º 3
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);
        }