Browse Source

Console demo is initially completed for microservice demo.

pull/746/head
Halil ibrahim Kalkan 7 years ago
parent
commit
718598fb8d
  1. 43
      samples/MicroserviceDemo/applications/consoleclient/ConsoleClientDemo/ClientDemoService.cs
  2. 16
      samples/MicroserviceDemo/microservices/identity/IdentityService.Host/IdentityServiceHostModule.cs
  3. 31
      samples/MicroserviceDemo/microservices/identity/IdentityService.Host/TestController.cs

43
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();
}
/// <summary>
/// Shows how to manually create an HttpClient and authenticate using the
/// IIdentityModelHttpClientAuthenticator service.
/// </summary>
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);
}
}
}
/// <summary>
/// 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.
/// </summary>
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)

16
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 =>

31
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<ActionResult> 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
);
}
}
}
Loading…
Cancel
Save