예제 #1
0
        public async Task LoginAsync(string username, string password) {
            const string Scheme = "Bearer";

            await Task.Run(() => {
                _context = new Microsoft.WindowsAzure.ActiveDirectory.Authentication.AuthenticationContext(Tenant.ToString());

                var providers = _context.GetProviders(Realm);
                var identity = providers.First(x => x.Name.Equals(Provider, StringComparison.InvariantCultureIgnoreCase));

                var credential = new UsernamePasswordCredential(identity.Name, username, password);

                _credential = _context.AcquireToken(Realm, identity, credential);

                var token = _credential.AsSecurityToken();
                var header = _credential.CreateAuthorizationHeader();

                _claims = token.Claims;

                _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Scheme, _credential.Assertion);

            }).ConfigureAwait(false);
        }
예제 #2
0
        AuthenticationResult GetAccessToken()
        {
            AuthenticationResult _authenticationResult = null;
            string resource = ConfigurationManager.AppSettings["ServiceRealm"];

            try
            {
                if (selectedIdentityProviderDescriptor == null)
                {
                    // Get the list of Idps
                    List <IdentityProviderDescriptor> idps = (List <IdentityProviderDescriptor>)_authenticationContext.GetProviders(resource);

                    if (idps.Count > 1)
                    {
                        // pop up a Home Realm Discovery window and let the user choose an Idp
                        ChooseIdp(idps);
                    }
                    else
                    {
                        selectedIdentityProviderDescriptor = idps[0];
                    }
                }

                // Invoke AuthenticationContext.AcquireToken to obtain an access token to access the Shipper service.
                // It will use a pop-up window to initiate the logon flow.
                _authenticationResult = _authenticationContext.AcquireToken(resource, selectedIdentityProviderDescriptor);

                ClearErrorLabel();

                if (_authenticationResult == null)
                {
                    DisplayUserNotAuthenticatedError();
                }
            }
            catch (ActiveDirectoryAuthenticationException ex)
            {
                string message = ex.Message;
                if (ex.InnerException != null)
                {
                    message += (" " + ex.InnerException.Message);
                }

                DisplayErrorMessage(message);
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex.Message);
            }

            return(_authenticationResult);
        }