EnableCrossAppRedirects - 跨域功能在哪里记录?

时间:2011-08-22 21:30:51

标签: c# asp.net forms-authentication

这里有一个有趣的ASP.NET FormsAuthentication功能,在这个SO答案中解释:How do you pass an authenticated session between app domains

快速摘要;您可以使用相同的加密密钥创建两个ASP.NET网站。 WebsiteA可以创建一个formauth令牌,并使用查询字符串(或POST正文)中的令牌重定向到WebsiteB。在WebsiteB和ASP.NET中启用EnableCrossAppRedirects会检测令牌并创建formauth cookie。在代码中:

FormsAuthentication.RedirectFromLoginPage("alice", true);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket("Alice", true, 30);
string encrypted = FormsAuthentication.Encrypt(ticket);
Response.Redirect("http://siteb.dev/Secure/WebForm1.aspx?" + FormsAuthentication.FormsCookieName + "=" + encrypted);

听起来很棒,但它在哪里记录?使用未记录的功能,我觉得感到不安。

我看过的地方 - 在任何MSDN参考中都没有提到这个功能。我想也许RedirectFromLoginPage会像我上面的代码一样构建一个重定向,但事实并非如此。

1 个答案:

答案 0 :(得分:15)

查看反射器后,表单身份验证(有些未记录)功能。启用EnableCrossAppRedirects时,除了查找auth cookie之外,.NET还会尝试从表单帖子或查询字符串中提取表单身份验证“cookie”。此代码嵌入在FormsAuthentication方法的ExtractTicketFromCookie类中,可以清楚地看到它在请求数据中查找身份验证cookie。

if (FormsAuthentication.EnableCrossAppRedirects)
{
    text = context.Request.QueryString[name];
    if (text != null && text.Length > 1)
    {
        if (!cookielessTicket && FormsAuthentication.CookieMode == HttpCookieMode.AutoDetect)
        {
            cookielessTicket = CookielessHelperClass.UseCookieless(context, true, FormsAuthentication.CookieMode);
        }
        try
        {
            formsAuthenticationTicket = FormsAuthentication.Decrypt(text);
        }
        catch
        {
            flag2 = true;
        }
        if (formsAuthenticationTicket == null)
        {
            flag2 = true;
        }
    }
    if (formsAuthenticationTicket == null || formsAuthenticationTicket.Expired)
    {
        text = context.Request.Form[name];
        if (text != null && text.Length > 1)
        {
            if (!cookielessTicket && FormsAuthentication.CookieMode == HttpCookieMode.AutoDetect)
            {
                cookielessTicket = CookielessHelperClass.UseCookieless(context, true, FormsAuthentication.CookieMode);
            }
            try
            {
                formsAuthenticationTicket = FormsAuthentication.Decrypt(text);
            }
            catch
            {
                flag2 = true;
            }
            if (formsAuthenticationTicket == null)
            {
                flag2 = true;
            }
        }
    }
}

因此,如果您在两个应用程序上启用EnableCrossAppRedirects,则第一个应用程序被授权重定向到外部站点,第二个应用程序将自动从请求中读取身份验证cookie。您只需要对其进行设计,以便返回登录URL可以发布cookie数据或将其发送到查询字符串中。您还需要确保机器密钥已同步,或者使用外部应用程序机器密钥(第一个应用程序)加密cookie。默认情况下,.NET会在您的查询字符串中发送加密的身份验证cookie,并且您的计算机密钥会同步(请参阅下面的MSDN引用)。

这里还有一些info on MSDN

  

如果CookiesSupported属性为true,则返回ReturnUrl   变量在当前应用程序内或   EnableCrossAppRedirects属性为true,然后是   RedirectFromLoginPage方法发出身份验证票证和   使用SetAuthCookie方法将其放在默认cookie中。

     

如果CookiesSupported为false且重定向路径为   当前应用程序,该票证是作为重定向URL的一部分发布的。   如果CookiesSupported为false,则EnableCrossAppRedirects为true,并且   重定向URL不引用当前应用程序中的页面,   RedirectFromLoginPage方法发出身份验证票证和   将它放在QueryString属性

对安全性的影响存在很大的警告。 EnableCrossAppRedirects是一种安全设置,可防止ASP.NET登录控件重定向到外部返回URL(另一个Web应用程序)。启用此设置后,它可以在某些形式的攻击中被利用 - 用户被发送到官方登录页面,但登录时会被重定向到他们可能认为相同的不同应用程序。这就是它默认禁用的原因。

启用此功能时,有助于缓解此问题的一种方法如下:

  

要在使用跨应用程序重定向时提高安全性,您应该这样做   覆盖RedirectFromLoginPage方法以仅允许重定向   批准的网站。

您还需要确保通过SSL提供重定向请求以保护传输中的“Cookie”,因为任何拦截都可以获得对该帐户的控制权。