简体   繁体   English

IdentityServer4基于IP地址的透明登录

[英]IdentityServer4 Transparent login based on Ip address

IdentityServer4 IP address transparent login IdentityServer4 IP地址透明登录

Hello, I'm working on a project that uses IdentityServer4 as Authentication/Authorization service. 您好,我正在使用IdentityServer4作为身份验证/授权服务的项目。 We have or own custom userstore injected in the service that we use to validate users based on credentials, and in the ProfileService we use the same userstore to decorate extra claims. 我们在用于基于凭据验证用户的服务中注入或拥有自定义用户存储,在ProfileService中,我们使用相同的用户存储来装饰额外的声明。 Now I have the assignment if it is possible, to do a transparent login based on an IP address, in our custom userstore some users have an ipaddress. 现在,如果可以的话,我可以进行分配,以便基于IP地址进行透明登录,在我们的自定义用户存储中,一些用户具有ipaddress。

The workflow goes like follows: when a user goes to our client applicatons (asp.net mvc), and the user is not authenticated, he's redirected to the IdentityServer, my first check here should be if I can resolve a user based on the incoming IP address, if so sign in and redirect to the client application, otherwise display the identityserver login page (based on the quickstart example). 工作流程如下:当用户转到我们的客户端应用程序(asp.net mvc),并且未通过身份验证时,该用户已重定向到IdentityServer,我在这里的第一个检查应该是我是否可以根据传入的请求解析用户IP地址(如果已登录)并重定向到客户端应用程序,否则显示Identityserver登录页面(基于快速入门示例)。

I've found a ticket in the github project related to impersonation, and possibly gets me a step closer? 我已经在github项目中找到了与模拟相关的票证,可能使我更近了一步? Based on override AuthorizeInteractionResponseGenerator https://github.com/IdentityServer/IdentityServer4/issues/853 基于重写AuthorizeInteractionResponseGenerator https://github.com/IdentityServer/IdentityServer4/issues/853

based on that ticket I've made some pseudo code, but I'm not sure if this is the correct way? 基于该票证,我已经做了一些伪代码,但是我不确定这是否是正确的方法?

 public class IPAuthorizeInteractionResponseGenerator: AuthorizeInteractionResponseGenerator
{
    private readonly ISystemClock _systemClock;
    private IProfileService _profileService;
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly IMyAccountUserStore _myAccountUserStore;

    public IPAuthorizeInteractionResponseGenerator(IMyAccountUserStore myAccountUserStore, IHttpContextAccessor httpContextAccessor, 
        ISystemClock clock, ILogger<AuthorizeInteractionResponseGenerator> logger, IConsentService consent, IProfileService profile) : base(clock, logger, consent, profile)
    {
        _systemClock = clock;
        _profileService = profile;
        _httpContextAccessor = httpContextAccessor;
        _myAccountUserStore = myAccountUserStore;
    }

    public override Task<InteractionResponse> ProcessInteractionAsync(ValidatedAuthorizeRequest request, ConsentResponse consent = null)
    {
        var clientIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
        var userByIp = _myAccountUserStore.FindUserByIp(clientIp);
        if(userByIp == null)
            return base.ProcessInteractionAsync(request, consent);



        //user found by ip ... 
        var claims = new[]
        {
            new Claim(JwtClaimTypes.Name, userByIp.Username),
            new Claim(JwtClaimTypes.GivenName, userByIp.FullName),
            new Claim(JwtClaimTypes.Email, userByIp.Email),
            new Claim(JwtClaimTypes.AuthenticationTime, _systemClock.UtcNow.DateTime.ToEpochTime().ToString())
        };

        var svr = new IdentityServerUser(userByIp.SubjectId) { AuthenticationTime = _systemClock.UtcNow.DateTime, AdditionalClaims = claims};
        var claimsPrincipal = svr.CreatePrincipal();
        request.Subject = claimsPrincipal;

        return Task.FromResult(new InteractionResponse());
    }
}

If you must do this solution (highly against it) would be a relatively simple level check. 如果必须(非常反对)执行此解决方案,则将是一个相对简单的级别检查。

in your login method 在您的登录方式中

  1. get user IP address (HttpContext.Connection.RemoteIpAddress.ToString()) 获取用户IP地址(HttpContext.Connection.RemoteIpAddress.ToString())
  2. check your user store for a user that has the allocated IP address 检查您的用户存储库中是否有分配了IP地址的用户
  3. if address exists then automatically login using that users credentials and respond with relevant response 如果地址存在,则使用该用户凭据自动登录并提供相关响应
  4. if address doesn't exist then load Login page for the ID4 server 如果地址不存在,则加载ID4服务器的“登录”页面

Note at bare minimum these things would need to be in place: 请注意,至少需要注意以下几点:

  • You will need to ensure your user store retains IP address for approved user 您将需要确保您的用户商店保留批准用户的IP地址
  • You will need to ensure your user store gets updated with new or updated details of user 您将需要确保使用新的或更新的用户详细信息更新您的用户商店

This should meet your requirements to use IP address for automatic login. 这应该满足您使用IP地址进行自动登录的要求。

Stressing the point that this approach should not in any way or form be used unless extreme set of requirements dictate this. 需要强调的一点是,除非极端要求,否则不应以任何方式或形式使用此方法。

IP Spoofing is so easy these days its not funny. 如今,IP欺骗是如此简单,这并不有趣。 Also if your user used a VPN that will also cause you issue, especially once you start building on geo-blocking. 另外,如果您的用户使用了也会引起您问题的VPN,尤其是当您开始构建地理封锁时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM