diff --git a/samples/MicroserviceDemo/applications/consoleclient/ConsoleClientDemo/ClientDemoService.cs b/samples/MicroserviceDemo/applications/consoleclient/ConsoleClientDemo/ClientDemoService.cs index a488d17635..3e817663d0 100644 --- a/samples/MicroserviceDemo/applications/consoleclient/ConsoleClientDemo/ClientDemoService.cs +++ b/samples/MicroserviceDemo/applications/consoleclient/ConsoleClientDemo/ClientDemoService.cs @@ -1,29 +1,68 @@ using System; +using System.Net.Http; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; using Volo.Abp.Identity; +using Volo.Abp.IdentityModel; namespace ConsoleClientDemo { public class ClientDemoService : ITransientDependency { private readonly IIdentityUserAppService _userAppService; + private readonly IIdentityModelHttpClientAuthenticator _authenticator; - public ClientDemoService(IIdentityUserAppService userAppService) + public ClientDemoService( + IIdentityUserAppService userAppService, + IIdentityModelHttpClientAuthenticator authenticator) { _userAppService = userAppService; + _authenticator = authenticator; } public async Task RunAsync() { + await TestWithHttpClient(); await TestIdentityService(); } + /// + /// Shows how to manually create an HttpClient and authenticate using the + /// IIdentityModelHttpClientAuthenticator service. + /// + private async Task TestWithHttpClient() + { + Console.WriteLine("*** TestWithHttpClient ***"); + + using (var client = new HttpClient()) + { + await _authenticator.AuthenticateAsync(client); + + var response = await client.GetAsync("http://localhost:63568/Test"); + if (!response.IsSuccessStatusCode) + { + Console.WriteLine(response.StatusCode); + } + else + { + var content = await response.Content.ReadAsStringAsync(); + Console.WriteLine(content); + } + } + } + + /// + /// Shows how to use application service interfaces (IIdentityUserAppService in this sample) + /// to call a remote service which is possible by the dynamic http client proxy system. + /// No need to use IIdentityModelHttpClientAuthenticator since the dynamic http client proxy + /// system internally uses it. You just inject a service (IIdentityUserAppService) + /// and call a method (GetListAsync) like a local method. + /// private async Task TestIdentityService() { var output = await _userAppService.GetListAsync(new GetIdentityUsersInput()); - Console.WriteLine("*** IdentityService ***"); + Console.WriteLine("*** TestIdentityService ***"); Console.WriteLine("Total user count: " + output.TotalCount); foreach (var user in output.Items) diff --git a/samples/MicroserviceDemo/microservices/identity/IdentityService.Host/IdentityServiceHostModule.cs b/samples/MicroserviceDemo/microservices/identity/IdentityService.Host/IdentityServiceHostModule.cs index b721b51449..636bb70aaa 100644 --- a/samples/MicroserviceDemo/microservices/identity/IdentityService.Host/IdentityServiceHostModule.cs +++ b/samples/MicroserviceDemo/microservices/identity/IdentityService.Host/IdentityServiceHostModule.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Swashbuckle.AspNetCore.Swagger; using Volo.Abp; @@ -13,6 +12,7 @@ using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.EntityFrameworkCore; +using Volo.Abp.Security.Claims; using Volo.Abp.SettingManagement.EntityFrameworkCore; namespace IdentityService.Host @@ -31,15 +31,21 @@ namespace IdentityService.Host { public override void ConfigureServices(ServiceConfigurationContext context) { - var configuration = context.Services.GetConfiguration(); - var x = configuration.GetConnectionString("Default"); - context.Services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { - options.Authority = "http://localhost:64999"; //TODO: Update + options.Authority = "http://localhost:64999"; options.RequireHttpsMetadata = false; options.ApiName = "IdentityService"; + + //TODO: Should create an extension method for that (may require to create a new ABP package depending on the IdentityServer4.AccessTokenValidation) + options.InboundJwtClaimTypeMap["sub"] = AbpClaimTypes.UserId; + options.InboundJwtClaimTypeMap["role"] = AbpClaimTypes.Role; + options.InboundJwtClaimTypeMap["email"] = AbpClaimTypes.Email; + options.InboundJwtClaimTypeMap["email_verified"] = AbpClaimTypes.EmailVerified; + options.InboundJwtClaimTypeMap["phone_number"] = AbpClaimTypes.PhoneNumber; + options.InboundJwtClaimTypeMap["phone_number_verified"] = AbpClaimTypes.PhoneNumberVerified; + options.InboundJwtClaimTypeMap["name"] = AbpClaimTypes.UserName; }); context.Services.AddSwaggerGen(options => diff --git a/samples/MicroserviceDemo/microservices/identity/IdentityService.Host/TestController.cs b/samples/MicroserviceDemo/microservices/identity/IdentityService.Host/TestController.cs new file mode 100644 index 0000000000..fd1a2b9755 --- /dev/null +++ b/samples/MicroserviceDemo/microservices/identity/IdentityService.Host/TestController.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.Json; + +namespace IdentityService.Host +{ + public class TestController : AbpController + { + private readonly IJsonSerializer _jsonSerializer; + + public TestController(IJsonSerializer jsonSerializer) + { + _jsonSerializer = jsonSerializer; + } + + [HttpGet] + public async Task Index() + { + var newLine = Environment.NewLine + Environment.NewLine; + + return Content( + "Claims: " + User.Claims.Select(c => $"{c.Type} = {c.Value}").JoinAsString(" | ") + newLine + + "CurrentUser: " + _jsonSerializer.Serialize(CurrentUser) + newLine + ); + } + } +}