asp.net - IdentityServer3 仅在移动设备上登录时持续重定向

标签 asp.net .net-core identityserver3

我遇到一个问题,我的身份服务器运行良好,在桌面计算机上登录用户时没有任何问题。但是,当我在移动应用程序上访问网页并登录时,我会遇到持续重定向的情况。

它第一次进入身份服务器,我登录,然后当它重定向回应用程序时,它会自动重定向回身份服务器并来回。

如果我停止重定向(通过点击浏览器上的停止按钮),然后转到我的网站,我现在已经登录了。

我正在使用 IdentityServer3 和 Asp.Net Core。

身份服务器的日志显示没有错误并且登录成功。如果我使用外部提供商或自定义提供商登录,就会发生这种情况。

我以为这是 safari 的问题,但我在手机上安装了 chrome,它也做了同样的事情。

我做了一些研究,我不认为这是一个 http/https 问题,而且我无法添加 Session_start,因为它在核心中不存在。

有人能想到移动应用程序无法运行而桌面应用程序运行正常的原因吗?对我可以检查的任何其他日志或我可以尝试的事情有什么建议吗?

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options =>
    {
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
    .AddCookie(options =>
    {
        options.LoginPath = new PathString("/Login/Login/");
        options.AccessDeniedPath = new PathString("/Login/Login/");
    })
    .AddOpenIdConnect(options =>
    {
        options.Authority = _authenticationServer;
        options.ClientId = "...";
        options.ResponseType = "id_token";
        options.Scope.Add("openid");
        options.Scope.Add("email");
        options.Scope.Add("profile");
        options.UseTokenLifetime = false;

        options.TokenValidationParameters = new TokenValidationParameters
        {
            NameClaimType = "name",
            ValidateIssuer = false,
        };
        options.Events = new OpenIdConnectEvents
        {
            OnTokenValidated = context =>
            {
                ...
                return Task.CompletedTask;
            }
        };
    });

    services.AddMvc(config =>
    {
        var policy = new AuthorizationPolicyBuilder()
                            .RequireAuthenticatedUser()
                            .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
    })
    .AddJsonOptions(options =>
            options.SerializerSettings.ContractResolver = new DefaultContractResolver());

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton(Configuration);
    services.AddMemoryCache();
    services.AddSession();
    services.AddKendo();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
    app.UseStaticFiles();
    app.UseSession();
    app.UseAuthentication();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
    });
}

更新:

我确认这只是 iPhone 上的问题。安卓运行良好。我还验证了这是 .Net Core 客户端应用程序的问题。 .Net 标准客户端应用程序运行良好。

我的授权端点被无限循环调用。

是否有人成功针对身份服务器设置了 .Net core 客户端应用程序并使其通过 iPhone 浏览器运行?有帮助吗?!?

最佳答案

我有一个解决方法来解决这个错误,我想发布这个错误,以防其他人遇到这个问题。

我怀疑任何尝试将 OpenIdConnect 与 Asp.Net Core 一起使用的人(不确定它必须是 IdentityServer)都会在 iPhone 上登录。

这只是一个解决方法,直到我能确切地找出它重定向的原因为止。我 Hook 重定向事件并检查是否存在原始 header 。第一次重定向到身份服务器时,已设置源,但第二次未设置。如果未设置,我检查它是否是手机。如果是这样,我会停止默认操作(不断重定向)并直接转到我的网址。

这是有效的,因为即使重定向,用户实际上也成功登录。这就是为什么这如此奇怪。

options.Events = new OpenIdConnectEvents
{
    OnRedirectToIdentityProvider = x =>
    {
        if (!string.IsNullOrWhiteSpace(x.Request.Headers["Origin"]))
        {
            string userAgent = x.Request.Headers["User-Agent"];
            if (IsMobileBrowser(userAgent))
            {
                x.HandleResponse();
                x.Response.Redirect(<url>);
            }
        }
        return Task.CompletedTask;
   },

关于asp.net - IdentityServer3 仅在移动设备上登录时持续重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53361553/

相关文章:

c# - 使用 Thinktecture IdentityServer3 进行身份验证/授权 native Android 应用程序的正确方法是什么

c# - Asp.net 和 C# 错误 : The name "mygv" does not exist in the current context

ASP.NET 打开弹出窗口然后重定向

c# - Configuration.GetSection ("SectionName") 在使用手动添加的 settings.json 文件到 dotnet 核心控制台应用程序时始终为 null

javascript - 隐式流和身份服务器 3 : Is it possible to grant a JavaScript client an access token without having the user log in?

angularjs - Identity Server 3 用户 session 生存期

c# - Catch Microsoft.Extensions.Caching.Redis 连接失败?

.net - 带有标准短语的免费/开源 resx 文件

json - .NET 核心 : Remove null fields from API JSON response

asp.net-core - 如何在 Visual Studio 2017 中打开 dotnet-core CLI 项目