WebForms FormsAuthentication - 身份验证后重定向导致登录页面再次调用

WebForms FormsAuthentication - Redirect after authentication results in Login Page to be called again

我在 2 个不同的域名下有 2 个 ASP.NET WebForms 项目。第一个只是存储重要文档的文件服务器。我正忙于实施 FormsAuthentication,以便如果有人在 Web 浏览器中输入文档的 URL,则会重定向到单独域名下的第二个 WebForms 项目的登录页面。我粘贴了第一个文件服务器项目的 Web.config 内容,然后粘贴了第二个项目的代码以授权下载文档。您会看到代码尚未完成。重定向到登录页面会发生什么。通过身份验证后,将重定向到文件,但文件服务器或第一个应用程序将此视为访问文件的新尝试并再次重定向到登录页面。就好像没有从文件服务器收到cookie。

非常感谢您的帮助。

  1. 应用程序 1 - 仅用于存储文件。

    <system.webServer>
        <modules>
            <remove name="FormsAuthentication" />
            <remove name="DefaultAuthentication" />
            <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" preCondition="" />
            <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" preCondition="" />
        </modules>
    </system.webServer>
    
  2. 应用程序 2 - 进行身份验证并定向到文件服务器上的文件(请注意,这两个应用程序位于不同的域中)

    protected void btnLogin_Click(object sender, EventArgs e) { 布尔验证=真; // 待实现

            if (Validated)
            {
                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, ".ASPXSAFECYTE", DateTime.Now, DateTime.Now.AddMinutes(30), true, "");
    
                string CookieString = FormsAuthentication.Encrypt(Ticket);
                HttpCookie Cookie = new HttpCookie(FormsAuthentication.FormsCookieName, CookieString);
    
                Cookie.Expires = Ticket.Expiration;
                Cookie.Path = FormsAuthentication.FormsCookiePath;
                Response.Cookies.Add(Cookie);
    
                if (Request["ReturnUrl"] == null)
                    Response.Redirect("~/Login.aspx", true);
                else
                {
                    string ReturnUrl = SystemProperties.FilesServerAddress.TrimEnd(new char[] { '/' }) + Request["ReturnUrl"];
                    Response.Redirect(ReturnUrl, true);
                }
            }
        }
    

亲切的问候,

贾科

出于安全目的,只能为托管域创建 Cookie and/or 它的子域。 因此,从一个域为另一个域创建 Cookie 是行不通的。