Exemplo n.º 1
0
        // login
        private async void button3_Click(object sender, EventArgs e)
        {
            var userName    = textBox2.Text;
            var pasWord     = textBox3.Text;
            var accessToken = await _authenticationService.GetAccessTokenAsync(
                new IdentityClientConfiguration(
                    _configuration["IdentityClients:Default:Authority"],
                    _configuration["IdentityClients:Default:Scope"],
                    _configuration["IdentityClients:Default:ClientId"],
                    _configuration["IdentityClients:Default:ClientSecret"],
                    _configuration["IdentityClients:Default:GrantType"],
                    //_configuration["IdentityClients:Default:UserName"],
                    userName,
                    //_configuration["IdentityClients:Default:UserPassword"]
                    pasWord
                    )
                );

            //var client = new HttpClient();
            //var disco = await client.GetDiscoveryDocumentAsync(_configuration["IdentityClients:Default:Authority"]);
            //if (disco.IsError)
            //{
            //    Console.WriteLine(disco.Error);
            //    return;
            //}
            //var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
            //{
            //    Address = disco.TokenEndpoint,
            //    ClientId = _configuration["IdentityClients:Default:ClientId"],
            //    ClientSecret = _configuration["IdentityClients:Default:ClientSecret"],
            //    //UserName = _configuration["IdentityClients:Default:UserName"],
            //    UserName = userName,
            //    //Password = _configuration["IdentityClients:Default:UserPassword"],
            //    Password = pasWord,
            //    Scope = _configuration["IdentityClients:Default:Scope"]
            //});
            //if (tokenResponse.IsError)
            //{
            //    Console.WriteLine(tokenResponse.Error);
            //    MessageBox.Show("Co loi xay ra");
            //    return;
            //}
            // Properties.Settings.Default.AccessToken = JsonSerializer.Serialize(tokenResponse);
            Properties.Settings.Default.AccessToken = accessToken;
            Properties.Settings.Default.Save();
            _form2.ShowDialog();
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetAccessToken(UserLoginInfo login)
        {
            await ReplaceEmailToUsernameOfInputIfNeeds(login);

            var config = new IdentityClientConfiguration
            {
                Authority    = _configuration["AuthServer:Authority"],
                ClientId     = _configuration["AuthServer:ClientId"],
                ClientSecret = _configuration["AuthServer:ClientSecret"],
                GrantType    = OidcConstants.GrantTypes.Password,
                UserName     = login.UserNameOrEmailAddress,
                UserPassword = login.Password,
                Scope        = "offline_access IdentityService BackendAdminAppGateway AuditLogging BaseManagement OrganizationService"
            };

            string token = await _authenticator.GetAccessTokenAsync(config);

            return(Json(new { code = 1, data = token }));
        }
Exemplo n.º 3
0
        /* Shows how to use HttpClient to perform a request to the HTTP API.
         * It uses ABP's IIdentityModelAuthenticationService to simplify obtaining access tokens.
         */
        private async Task TestWithHttpClientAndIdentityModelAuthenticationServiceAsync()
        {
            Console.WriteLine();
            Console.WriteLine($"***** {nameof(TestWithHttpClientAndIdentityModelAuthenticationServiceAsync)} *****");

            //Get access token using ABP's IIdentityModelAuthenticationService

            var accessToken = await _authenticationService.GetAccessTokenAsync(
                new IdentityClientConfiguration(
                    _configuration["IdentityClients:Default:Authority"],
                    _configuration["IdentityClients:Default:Scope"],
                    _configuration["IdentityClients:Default:ClientId"],
                    _configuration["IdentityClients:Default:ClientSecret"],
                    _configuration["IdentityClients:Default:GrantType"],
                    _configuration["IdentityClients:Default:UserName"],
                    _configuration["IdentityClients:Default:UserPassword"]
                    )
                );

            Console.WriteLine(accessToken);
            //Perform the actual HTTP request

            using (var httpClient = new HttpClient())
            {
                httpClient.SetBearerToken(accessToken);

                var url = _configuration["RemoteServices:LoginOauth2Demo:BaseUrl"] +
                          "api/LoginOauth2Demo/sample/authorized";

                var responseMessage = await httpClient.GetAsync(url);

                if (responseMessage.IsSuccessStatusCode)
                {
                    var responseString = await responseMessage.Content.ReadAsStringAsync();

                    Console.WriteLine("Result: " + responseString);
                }
                else
                {
                    throw new Exception("Remote server returns error code: " + responseMessage.StatusCode);
                }
            }
        }