gpt4 book ai didi

asp.net - 如何在 SessionSecurityTokenCreated 时有条件地设置 FedAuth cookie SessionToken.IsPersistent

转载 作者:行者123 更新时间:2023-12-02 14:30:14 24 4
gpt4 key购买 nike

我将 WIF 与 WS Federation 结合使用,以便我的 ASP.NET 应用程序可以针对 STS (Thinktecture IdentityServer) 进行身份验证。在我的 RP 中,我想根据用户的声明实用地设置 cookie 持久性。

观察 Fiddler 中的流量,我可以看到当 STS token 发布到 RP 时,首先设置 WIF FedAuth cookie。在设置 cookie 之前,我想拦截一些事件,并根据当前声明将 cookie 设置为持久(或不持久)。

我知道我可以在 web.config 中设置 cookie 持久性,但是此行为需要基于用户的条件。

<罢工>

<罢工>
<wsFederation ... persistentCookiesOnPassiveRedirects="true" />

<罢工>

我的第一个方法是尝试处理各种 SessionSecurityTokenCreated 事件,但这些事件似乎从未被触发。 我是否错误地添加了处理程序?或者有更好的方法吗?

protected void Application_Start()
{
...

FederatedAuthentication.SessionAuthenticationModule.SessionSecurityTokenCreated +=
new EventHandler<SessionSecurityTokenCreatedEventArgs>(SessionAuthenticationModule_SessionSecurityTokenCreated);

FederatedAuthentication.WSFederationAuthenticationModule.SessionSecurityTokenCreated +=
new EventHandler<SessionSecurityTokenCreatedEventArgs>(WSFederationAuthenticationModule_SessionSecurityTokenCreated);

}



//This never seems to fire...
void SessionAuthenticationModule_SessionSecurityTokenCreated(object sender,
SessionSecurityTokenCreatedEventArgs e)
{
if (e.SessionToken.ClaimsPrincipal.HasClaim("someClaim", "someValue"))
e.SessionToken.IsPersistent = true;
else
e.SessionToken.IsPersistent = false;
}


//This never seems to fire either...
void WSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender,
SessionSecurityTokenCreatedEventArgs e)
{
if (e.SessionToken.ClaimsPrincipal.HasClaim("someClaim", "someValue"))
e.SessionToken.IsPersistent = true;
else
e.SessionToken.IsPersistent = false;

}

有趣的是:如果我为 SessionAuthenticationModule_SessionSecurityTokenReceived 添加处理程序,此事件似乎会触发。在这里,我可以重新发出 cookie 并设置 IsPersistent = true,但是直到首次设置 cookie 后才会触发此操作,我更愿意在首次发出 cookie 时执行此操作。

经过一番测试:如果我在 SessionAuthenticationModule_SessionSecurityTokenReceived 中重新发出 cookie,那么 SessionAuthenticationModule_SessionSecurityTokenCreated 将被触发。我似乎无法找出为什么当 token 首次发布到 RP 时,在 cookie 的初始创建时不会触发此操作。

最佳答案

我的问题的根源是: a) 我使用的是自定义 WSFederationAuthenticationModule。 b) 我没有使用自定义模块的名称连接 Global.asax 中的事件。

假设我的 web.config 中有这个:

<system.webServer>

// ...

<add name="MyCustomWSFederationAuthenticationModule"
type="MyLib.MyCustomWSFederationAuthenticationModule, Thinktecture.IdentityModel, Version=1.0.0.0, Culture=neutral"
preCondition="managedHandler" />

<add name="SessionAuthenticationModule"
type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
preCondition="managedHandler" />


// ...

</system.webServer>

假设“MyCustomWSFederationAuthenticationModule”是自定义 Fed-auth 模块的名称。然后我只需修复方法处理程序的名称(应用程序启动中没有任何内容)。

protected void Application_Start()
{
//Nothing here.
}

//This never seems to fire either...
void MyCustomWSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender,
SessionSecurityTokenCreatedEventArgs e)
{
if (e.SessionToken.ClaimsPrincipal.HasClaim("someClaim", "someValue"))
e.SessionToken.IsPersistent = true;
else
e.SessionToken.IsPersistent = false;
}

关于asp.net - 如何在 SessionSecurityTokenCreated 时有条件地设置 FedAuth cookie SessionToken.IsPersistent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293939/

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