簡體   English   中英

反向代理后面的 ASP.NET MVC 應用程序 - 身份驗證后使用錯誤的主機名

[英]ASP.NET MVC application behind reverse proxy - using wrong host name after auth

我有一個使用 OWIN 身份驗證的 ASP.NET MVC 應用程序,它在反向代理后面運行。

ASP.NET 中的身份驗證設置如下:

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,

            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

iis 中的反向代理在 web.config 中是這樣設置的:

<system.webServer>
<httpErrors errorMode="Detailed" />
<asp scriptErrorSentToBrowser="true"/>
    <rewrite>
            <rule name="proxy" stopProcessing="true">
                <match url="^app/?(.*)" />
                <serverVariables>
                    <set name="X_REQUESTED_URL_PATH" value="{R:1}" />
                </serverVariables>
                <action type="Rewrite" url="https://myapp.mydomain.toplevel/app/{R:1}" />
            </rule>
    </rewrite>
<system.webServer>

反向代理托管在https://www.mydomain.toplevel/app/ {R:1}

一切正常,RedirectToAction 將重定向到 www.mydomain.toplevel。

但是當我嘗試使用 AuthenticationAttribute 打開控制器時,重定向將轉到https://myapp.mydomain.toplevel/account/login而不是 www.mydomain.toplevel

我如何配置我的應用程序保持在反向代理之后,即使正在發生身份驗證重定向? 作為第一個解決方法,我嘗試使用前面的主機名對 LoginPath 進行硬編碼,但這將給出路徑應以 / 開頭的錯誤。

事實證明,這很容易解決。 我剛剛在 AuthenticationProvider 上實現了我自己的 OnApplyRedirect 方法:

var provider = new CookieAuthenticationProvider
{
    // ..
};

provider.OnApplyRedirect = context =>
{
    UrlHelper _url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
    String actionUri = _url.Action("Login", "Account", new { ReturnUrl = context.Request.Uri.PathAndQuery });
    context.Response.Redirect(actionUri);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM