我在 2 個不同的域名下有 2 個 ASP.NET WebForms 專案。第一個僅僅是存盤重要檔案的檔案服務器。我正忙于實作 FormsAuthentication,因此如果有人在 Web 瀏覽器中輸入檔案的 URL,則會重定向到另一個 WebForms 專案的登錄頁面,該頁面位于單獨的域名下。我已經粘貼了第一個檔案服務器專案的 Web.config 內容,然后粘貼了第二個專案的代碼以授權下載檔案。您將看到代碼尚未完成。重定向到登錄頁面會發生什么。經過身份驗證后,重定向到檔案,但檔案服務器或第一個應用程式將此視為訪問檔案的新嘗試并再次重定向到登錄頁面。就好像沒有從檔案服務器接收到 cookie。
非常感謝您的幫助。
應用程式 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 - 進行身份驗證并定向到檔案服務器上的檔案(請注意,兩個應用程式位于不同的域中)
protected void btnLogin_Click(object sender, EventArgs e) { bool Validated = true; // 還有待實作
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); } } }
親切的問候,
雅科
uj5u.com熱心網友回復:
出于安全考慮,只能為托管域和/或其子域創建 Cookie。因此,從一個域為另一個域創建 Cookie 將不起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/432762.html
