【问题标题】:Get IP address when implementing IResourceOwnerPasswordValidator实现 IResourceOwnerPasswordValidator 时获取 IP 地址
【发布时间】:2017-01-11 19:18:36
【问题描述】:

在使用 c# 实现 IdentityServer4 的 IResourceOwnerPasswordValidator 时,有人知道如何获取远程 IP 地址吗?

***** 编辑

多亏了让我走上正轨的最小特权,我现在得到了以下信息:

我的 UserValidationService [IResourceOwnerPasswordValidator] 类有:

public UserValidationService(ISecurity SecurityRepo, IHttpContextAccessor HttpContext)
{
        _securityRepo = SecurityRepo;
        _httpContext = HttpContext;
}

我的 setup.cs 类有:

var identityServer = services.AddIdentityServer()
                                .AddSigningCredential(key)
                                .AddInMemoryPersistedGrants()                                 .AddInMemoryIdentityResources(Config.GetIdentityResources())                                    .AddInMemoryApiResources(Config.GetApiResources())
                                .AddInMemoryClients(Config.GetClients());


identityServer.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
identityServer.Services.AddTransient<IProfileService, ProfileService>();            
                identityServer.Services.AddTransient<IResourceOwnerPasswordValidator>(x => new UserValidationService(bllFactory.GetInstance<ISecurity>()));

我收到一个错误

没有给出与 'UserValidationService.UserValidationService(ISecurity, IHttpContextAccessor) 的所需形式参数 'HttpContext' 对应的参数

我理解错误信息,但是我在网上看到的示例没有手动发送 IHttpContextAccessor。

谢谢

大卫

【问题讨论】:

    标签: c# identityserver4


    【解决方案1】:

    您可以将IHttpContextAccessor 注入到您的实现中。

    从那里您可以访问 HttContext - 然后访问请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 2011-10-11
      • 1970-01-01
      • 2018-05-08
      • 2013-01-16
      相关资源
      最近更新 更多