c# - 如何在 ASP.NET Core 身份验证中将 OAuth 回调配置到不同的域

标签 c# asp.net-core oauth-2.0 asp.net-identity

我正在针对 OAuth 端点进行身份验证,我只能在其中配置 1 个回调域。 (本地主机已列入白名单)。

我的 Web 应用程序在 Azure (myapp.azurewebsites.net) 中运行,并可用于两个自定义域(myapp.cc 和 myapp.eu)。当我使用默认设置时,CallbackPath 只能是相对路径(到当前域)

CallbackPath 的代码文档表明它是相对于应用程序的基本路径:

/// <summary>
/// The request path within the application's base path where the user-agent will be returned.
/// The middleware will process this request when it arrives.
/// </summary>
public PathString CallbackPath { get; set; }

我想确保回调发生在我在 OAuth 后端列入白名单的(唯一)域中。我知道我可以手动实现所有内容,但我希望有一种简单的方法来解决此设计并仍然受益于内置的身份验证选项。

因此,即使用户正在登录 myapp.cc 或 myapp.eu 或 myapp.azurewebsites.net,它也应该重定向到 myapp.azurewebsites.net/(在我的 Auth 服务中列入白名单)

下面粘贴了我的 Startup.cs 文件的一部分:

services.AddAuthentication(options =>
    {
        options.DefaultChallengeScheme = "MyService";
    })
    .AddCookie()
    .AddOAuth("MyService", "MyService",
        options =>
        {
            options.ClientId = settings.ClientId;
            options.ClientSecret = settings.ClientOauthSecret;
            options.CallbackPath = "/relativeonlypath"; 

            options.SaveTokens = true; 

            options.SignInScheme = IdentityConstants.ExternalScheme;

            /// ...  removed for brevity
        }
    );

关于如何实现这个的任何想法?

谢谢

最佳答案

我不确定这是否可能,因为要验证用户是否作为“真正”身份验证流程的一部分重定向到您的应用程序,ASP.NET OAuth 处理程序执行以下步骤:

  1. 在将用户重定向到 OAuth 服务之前,ASP.NET Core 会生成一个绑定(bind)到当前域的“关联”cookie;和
  2. 当用户被重定向到应用程序时,处理程序会查找此 cookie 并验证其内容。

因此,如果关联 cookie 在步骤 #1 中为一个域生成,比方说 myapp.cc,用户将被重定向到另一个域,myapp.azurewebsites.net,ASP.NET Core 可能无法读取它,因为浏览器不会将其包含在重定向请求中。

Note

As seen in the first comments, the original thought was to leverage the SameSiteproperty of the correlation cookie to have it sent by the browser to the second domain. This was all wrong, apologies!

我现在认为您有 2 个不同的选择:

  1. 将来自myapp.ccmyapp.eu每个 请求重定向到myapp.azurewebsites.net,所以当身份验证流程发生时,我们已经在正确的域中;或
  2. 在将用户重定向到 OAuth 服务器之前,将用户重定向到 myapp.azurewebsites.net 域。

我不会讨论第一个解决方案,因为有很多方法可以实现。

下面是一些我尚未测试过的代码,但可以用于第二种解决方案:

services
    .AddAuthentication(options =>
    {
        options.DefaultChallengeScheme = "MyService";
    })
    .AddCookie()
    .AddOAuth("MyService", options =>
    {
        options.Events.OnRedirectToAuthorizationEndpoint = context =>
        {
            var currentRequestUri = new Uri(context.Request.GetDisplayUrl());

            // 1. If we're not on the correct domain, redirect the user to the same page, but on the expected domain.
            // The assumption is that the authentication flow will also kick in on the other domain (see 2).
            if (currentRequestUri.Host != "myapp.azurewebsites.net")
            {
                var applicationRedirectUri = new UriBuilder(currentRequestUri)
                {
                    Host = "myapp.azurewebsites.net"
                }.Uri.ToString();

                context.Response.Redirect(applicationRedirectUri);
                return Task.CompletedTask;
            }

            // 2. If we reach here, it means we're on the right domain, so we can redirect to the OAuth server.
            context.Response.Redirect(context.RedirectUri);
            return Task.CompletedTask;
        };
    });

关于c# - 如何在 ASP.NET Core 身份验证中将 OAuth 回调配置到不同的域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65932391/

相关文章:

c# - backgroundworker 完成的事件处理程序在哪个线程上运行?

c# - Entity Framework : Unknown column in 'where clause

c# - Paypal Rest API 使用模拟账户但不使用真实账户

c# - CustomWebViewRenderer DownloadManager 与 CopyBackForwardList 的问题

asp.net-core - 安装 vue N​​uGet 包后找不到任何要链接的 vue.js 文件

security - OAuth 2.0 中的客户端 key

c# - ASP.NET 核心 2.2 : Unable to resolve service for type 'AutoMapper.IMapper'

dependency-injection - 使用 ITypeConverter 进行 AutoMapper 依赖注入(inject)

java - 交换授权码只能使用一次 - Oauth 2.0

java - 使用 google api java 登录