【发布时间】:2020-05-05 14:29:37
【问题描述】:
我有两个网站,都在不同的域中。所以例如http://example1.com 和 http://example2.com。这两个站点都在 ASP .NET 中并使用 Forms 身份验证。 example1.com 的用户需要访问 example2.com 的某些资源,而无需在 example2.com 中拥有帐户。因此,每当 example1.com 的用户需要访问 example2.com 中的资源时,都会生成一个临时 URL,用户可以通过该 URL 访问该资源。该 URL 是限时使用的 URL,在第一次使用后失效。这是它的工作原理。
- 当 example1.com 需要访问 example2.com 端的资源时,会发生 response.redirect,并在查询字符串中使用加密令牌。例如http://example2.com/path/resource1/?token=encryptedToken
- 此令牌在 example2.com 端读取,如果有效,则表单身份验证设置为“临时用户”。 “临时用户”是专为 example.com 用户创建的受限访问用户。
下面是在 Application_AuthenticateRequest 事件中实现这一点的代码
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
const string TempUser = "TempUser";
if (!Request.IsAuthenticated)
{
var url = Request.Url;
var token = Request.QueryString["token"];
if(IsKnownUrl(url) && IsValidToken(token))
{
FormsAuthentication.SetAuthCookie(TempUser, false);
Response.Redirect(Request.RawUrl);
}
}
}
private bool IsKnownUrl(string url){
//Logic to check the url is from known sources
}
private bool IsValidToken(string token){
//Logic to token is valid and not expired
}
在两个站点都切换到 SSL 之前,这一切正常。即https://example1.com 和https://example2.com。这停止了工作。从 example1.com 到 example2.com 的 Response.Redirect 进入 302 循环,这意味着由于某种原因未设置或接受身份验证 cookie。当我在 Chrome 浏览器开发者工具中查看网络活动时,我看到了以下线索
姓名状态
https://example2.com/path/resource1/?token=encryptedToken 302
https://example2.com/path/resource1/?token=encryptedToken 302
https://example2.com/path/resource1/?token=encryptedToken 302
........
浏览器终于放弃了,提示页面重定向次数过多。
对可能出现的问题有任何想法吗?
【问题讨论】:
-
您是否使用 Fiddler 检查过发生了什么,例如是否设置了 cookie? (如果您不知道 Fiddler 是什么,它是一个反向代理,它记录来自您的系统的原始 http 请求和响应、标头、正文等所有内容)。此外,您可能已经这样做了,但只需仔细检查您的所有 url 引用也已更新为 HTTPS,并且您设置的 cookie 的 Secure 属性设置为 true (此设置不应该有所作为,但只是尝试无论如何)。
标签: c# asp.net security cross-domain forms-authentication