gpt4 book ai didi

asp.net - 如何跟踪过期的 WIF fedauth cookie?

转载 作者:行者123 更新时间:2023-12-04 17:48:52 24 4
gpt4 key购买 nike

我在尝试跟踪过期的 WIF 身份验证 session /cookie 时遇到了一个有趣的问题。

作为背景知识:该站点是 MVC 3,使用与 ADFS 服务器作为 STS 的信任的 Windows Identity Foundation (WIF)。整个站点受 SSL 保护。 STS 将 token 到期时间设置为 60 分钟。

当用户手动注销时,我们只需调用 FedAuth 模块上的 SignOut 方法:

FederatedAuthentication.WSFederationAuthenticationModule.SignOut(false);

这当然会删除 FedAuth cookie,但这就是问题所在。如果我使用 Fiddler 捕获这些 cookie,我可以在它们的到期时间内将它们重新呈现给站点,并且仍然被视为已登录。

我意识到这是从已接受 fiddler 作为代理的浏览器的特权位置执行的......但客户担心那些实际上没有过期的身份验证 cookie 会带来重大的安全风险。他们不相信 SSL 可以充分保护站点,并且如果攻击者可以执行 MITM 攻击,他们可以在用户认为他们已经注销后使用这些 cookie。

我已经解释过,如果他们在注销后易受攻击,他们在登录期间也是易受攻击的,但他们不在乎......

因此,我一直在寻找方法来确保一旦用户注销,与该登录 session 关联的 fedauth cookie 将被视为过期。 WIF 处理程序似乎没有用于跟踪过期 token 的内置机制,我还没有找到与此相关的任何其他内容。

我想这实际上是一个更广泛的问题-> 一般如何检测过期的 cookie?有效的 cookie 就是有效的 cookie!

显而易见的解决方案是在注销后以某种方式跟踪这些 cookie,但如果可能的话,我想避免自定义代码路由;作为一个菜鸟,很多安全文献都说要避免自定义编码任何类型的 session 机制,因为你可能会弄错!

有人知道 ASP.NET 中针对此问题的任何标准解决方案吗?

提前致谢。

最佳答案

我们的安全团队向我提出了类似的要求。我选择将 asp.net session ID 存储在 OWIN cookie 中,并且在 cookie 中包含 session ID 的每个请求上,我验证它与事件 session 的 ID 匹配。

在第一个经过身份验证且 cookie 中还没有 session id 的请求结束时,将 session id 存储在 cookie ( adapted from this answer ) 中:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);

bool authenticated = User.Identity.IsAuthenticated;

var sessionGuid = (User as ClaimsPrincipal).FindFirst("sessionID")?.Value;

//put the SessionID into the cookie.
if (authenticated && string.IsNullOrEmpty(sessionGuid))
{
var id= Session.SessionID;

//update the guid claim to track with the session
var authenticationManager = HttpContext.GetOwinContext().Authentication;

// create a new identity from the old one
var identity = new ClaimsIdentity(User.Identity);

// update claim value
identity.RemoveClaim(identity.FindFirst("sessionID"));
identity.AddClaim(new Claim("sessionID", id));

// tell the authentication manager to use this new identity
authenticationManager.AuthenticationResponseGrant =
new AuthenticationResponseGrant(
new ClaimsPrincipal(identity),
new AuthenticationProperties { IsPersistent = true }
);
}
}

然后在以后的每个请求中,如果我在 cookie 中找到 session ,将其与事件 session 进行比较。如果它们不匹配,则注销:
protected override void OnActionExecuting( ActionExecutingContext filterContext)
{
var claim = (User as ClaimsPrincipal).FindFirst("sessionID")?.Value;

//does the owin cookie have a sessionID?
if (!string.IsNullOrEmpty(claim))
{
string session = Session.SessionID;

//does it match the one stored in the session?
if(session != claim)
{
//no? log the user out again..
Session.Abandon();

//redirect to logged out page
this.Request.GetOwinContext().Authentication.SignOut();

//tell them its over..
Response.Write("Expired Session");

Response.End();
}
}

base.OnActionExecuting(filterContext);
}

关于asp.net - 如何跟踪过期的 WIF fedauth cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23049478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com