我有服务,其中一个用于授权和身份验证(身份服务 - localhost:7096),它使用身份系统配置,另一个用于与身份验证用户共享资源(应用程序服务 -localhost:7141)。
当我尝试使用 RPC 模型 RabbitMq 将已登录用户的名称从身份服务获取到应用程序服务时,问题就开始了。
所以我首先要做的是使用 IdentityServices 中的端点来获取已登录用户的名称
private async void ConsumerReceived(object sender, BasicDeliverEventArgs e)
{
var response = "";
var body = e.Body.ToArray();
var props = e.BasicProperties;
var replyProps = _channel.CreateBasicProperties();
replyProps.CorrelationId = props.CorrelationId;
try
{
var wee = _serviceProvider.GetService<IHttpClientFactory>();
var client = wee.CreateClient();
var loggedUserName = await client.GetAsync("https://localhost:7096/get-logged-in-user-name");
response = await loggedUserName.Content.ReadAsStringAsync();
}
catch (Exception E)
{
Console.WriteLine(E);
response = "null";
}
finally
{
var responseBytes = Encoding.UTF8.GetBytes(response);
_channel.BasicPublish(exchange: "",
routingKey: props.ReplyTo,
basicProperties: replyProps,
body: responseBytes);
_channel.BasicAck(deliveryTag: e.DeliveryTag,
multiple: false);
}
}
此方法由具有端点get-logged-in-user-name 的控制器使用以返回名称
public static class ClaimPrincipleExtension
{
public static string GetUsername(this ClaimsPrincipal user)
{
return user.FindFirst(ClaimTypes.Name).Value;
}
}
奇怪的是,我注意到当我激活应用程序服务中的端点以向 rpc 服务器(位于 IdentityService 中)发送请求时,它会尝试返回登录用户名的字符串以将其传递给应用程序服务,但是在上面的项目中,我得到了下面显示的相同错误,但是当我在控制器中键入原始字符串以返回时,一切正常。 GetUsername - 当我在 IdentityService 中直接调用它时有效,但是当我尝试使用应用程序服务调用它时它不起作用
[HttpGet("get-logged-in-user-name")]
public string GetLoggedInUserName()
{
string username = User.GetUsername(); //!< -get current logged in userName
return username; //!< -this invoke error in rpc Server and Client .
// return "ExampleUser"; //! <- with this all work fine
}
错误:
在 /Users/michal/Desktop/MyDateApp/Shared/Extensions/ClaimPrincipleExtension.cs 中的 Shared.Abstraction.Extensions.ClaimPrincipleExtension.GetUsername(ClaimsPrincipal 用户):第 10 行
在 IdentityServer.Application.Controllers.UserController.GetLoggedInUserName() 在 /Users/michal/Desktop/MyDateApp/MicroServices/IdentityServer/IdentityServer.Application/Controllers/UserController.cs:line 41 在 lambda_method3(闭包,对象,对象[]) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper 映射器、ObjectMethodExecutor 执行器、对象控制器、对象 [] 参数) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next、Scope& scope、Object& state、Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() --- 从上一个位置开始的堆栈跟踪结束 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed 上下文) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next、Scope& scope、Object& state、Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- 从上一个位置开始的堆栈跟踪结束 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|25_0(ResourceInvoker 调用程序、任务 lastTask、接下来的状态、范围范围、对象状态、布尔值已完成) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed 上下文) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next、Scope& scope、Object& state、Boolean& isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- 从上一个位置开始的堆栈跟踪结束 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker 调用程序、任务任务、IDisposable 范围) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker 调用程序、任务任务、IDisposable 范围) 在 Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(端点端点、任务请求任务、ILogger 记录器) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext 上下文) 在 Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext 上下文) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
主机:本地主机:7096`