系统相关
首页 > 系统相关> > C#-Windows Identity Foundation安全令牌服务无法保持登录状态

C#-Windows Identity Foundation安全令牌服务无法保持登录状态

作者:互联网

我正在使用Windows Identity Foundation(WIF)安全令牌服务(STS)来处理我的应用程序的身份验证,该应用程序运行良好.但是,我似乎无法长期使用STS登录.

据我了解,我不应该在应用程序级别上关心客户端令牌,因为它们可以使它们想要的所有内容都过期,它应该将我重定向到STS,并且只要它们仍然登录在STS上,它就应该刷新其应用程序令牌.但是,似乎并不想让他们登录.

这是我在STS上的login.aspx中发生的情况

var cookie = FormsAuthentication.GetAuthCookie(userName, persistTicket);

if (persistTicket)
    cookie.Expires = DateTime.Now.AddDays(14);

Response.Cookies.Add(cookie);

var returnUrl = Request.QueryString["ReturnUrl"];
Response.Redirect(returnUrl ?? "default.aspx");

这几乎是使用常规Forms Auth直接从现有应用程序中获取的.

从我的web.config

<authentication mode="Forms">
      <forms loginUrl="Login.aspx" protection="All" timeout="2880" 
      name=".STS" path="/" requireSSL="false" slidingExpiration="true" 
      defaultUrl="default.aspx" cookieless="UseDeviceProfile" 
      enableCrossAppRedirects="false" />
</authentication>

登录后查看cookie,可以看到cookie的过期时间设置为将来的14天,并且cookie不是会话cookie.

当我需要重新登录到STS时,可以看到我原来的cookie仍然在那里.

即使据我所知,STS是否有某种时间戳功能嵌入到cookie中,从而使我的cookie无效?

解决方法:

阅读@uosel的建议后,这使我沿着对此处实际发生的内容进行更深入分析的路径.而我的目标是仅为STS本身而不为STS消耗站点创建持久性cookie.这样,在任何使用STS的站点到期时,我始终可以在STS级别上验证用户.

随着这种思路的不断发展,我意识到STS入门站点使用表格身份验证来保护在index.aspx中发生的实际WIF授权.问题是我没有逻辑可以使用现有的表单身份验证票证来处理向表单身份验证安全索引页面的传输.

这导致我找到类似于

if(User.Identity.IsAuthenticated)
{    
    if(IsValidUserCredentials())
    {
       var returnUrl = Request.QueryString["ReturnUrl"];
       Response.Redirect(returnUrl ?? "default.aspx");
    }    
}
else
{
    DisplayLoginForm()
}

标签:wif,sts-securitytokenservice,asp-net,c,stay-logged-in
来源: https://codeday.me/bug/20191209/2096974.html