我正在使用基于WIF(.NET 4.0)的自定义STS,该STS目前仅用于SharePoint应用程序。我在HTTP模块中设置了滑动过期代码,该模块按预期工作,但安全令牌的生命周期为10小时(默认生命周期)。
/// <summary>
/// Handles the SessionSecurityTokenReceived event of the SingleSignOnModule control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Microsoft.IdentityModel.Web.SessionSecurityTokenReceivedEventArgs"/> instance containing the event data.</param>
private void SingleSignOnModule_SessionSecurityTokenReceived(Object sender, SessionSecurityTokenReceivedEventArgs e)
{
using (new SPMonitoredScope("SingleSignOnModule-SessionSecurityTokenReceived"))
{
if ((HttpContext.Current != null) && (FederatedAuthentication.SessionAuthenticationModule != null) && (e != null))
{
TimeSpan logonTokenCacheExpirationWindow = TimeSpan.FromSeconds(1);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
logonTokenCacheExpirationWindow = SPSecurityTokenServiceManager.Local.LogonTokenCacheExpirationWindow;
});
DateTime currentDateTime = DateTime.UtcNow;
TimeSpan sessionLifetime = (e.SessionToken.ValidTo - e.SessionToken.ValidFrom);
DateTime sessionValidFrom = e.SessionToken.ValidFrom;
DateTime sessionValidTo = (e.SessionToken.ValidTo - logonTokenCacheExpirationWindow);
if ((currentDateTime < sessionValidTo) && (currentDateTime > sessionValidFrom.AddMinutes(sessionLifetime.TotalMinutes / 2)))
{
e.SessionToken = FederatedAuthentication.SessionAuthenticationModule.CreateSessionSecurityToken(e.SessionToken.ClaimsPrincipal, e.SessionToken.Context, currentDateTime, currentDateTime.AddMinutes(sessionLifetime.TotalMinutes), e.SessionToken.IsPersistent);
e.ReissueCookie = true;
}
}
}
}
最初,我认为这可以由SPSecurityTokenServiceManager设置。但是,这没有改变。 (PowerShell代码段)
Write-Output("[INFO] Updating the SPSecurityTokenServiceManager")
$stsMgr = Get-SPSecurityTokenServiceConfig
Write-Output("[INFO] Updating the SPSecurityTokenServiceManager to use session cookies.")
$stsMgr.UseSessionCookies = $true; #
Write-Output("[INFO] Updating the SPSecurityTokenServiceManager logon token cache expiration window")
$stsMgr.LogonTokenCacheExpirationWindow = New-TimeSpan -Days 0 -Hours 0 -Minutes 1
Write-Output("[INFO] Updating the SPSecurityTokenServiceManager service token cache expiration window.")
$stsMgr.ServiceTokenCacheExpirationWindow = New-TimeSpan -Days 0 -Hours 0 -Minutes 20
$stsMgr.Update()
我无法设置SessionSecurityTokenHandler.DefaultLifetime,因为它是只读的,并设置为10小时。
// Type: Microsoft.IdentityModel.Tokens.SecurityTokenHandler
// Assembly: Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:\Windows\assembly\GAC_MSIL\Microsoft.IdentityModel\3.5.0.0__31bf3856ad364e35\Microsoft.IdentityModel.dll
namespace Microsoft.IdentityModel.Tokens
{
public class SessionSecurityTokenHandler : SecurityTokenHandler
{
public static readonly TimeSpan DefaultLifetime = TimeSpan.FromHours(10.0);
...
}
}
SecurityToken.ValidTo只有一个getter而不是setter。
// Type: System.IdentityModel.Tokens.SecurityToken
// Assembly: System.IdentityModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// Assembly location: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.IdentityModel.dll
namespace System.IdentityModel.Tokens
{
/// <summary>
/// Represents a base class used to implement all security tokens.
/// </summary>
/// <filterpriority>2</filterpriority>
public abstract class SecurityToken
{
...
/// <summary>
/// Gets the last instant in time at which this security token is valid.
/// </summary>
///
/// <returns>
/// A <see cref="T:System.DateTime"/> that represents the last instant in time at which this security token is valid.
/// </returns>
/// <filterpriority>2</filterpriority>
public abstract DateTime ValidTo { get; }
...
}
}
我还注意到在FederatedAuthentication.SessionAuthenticationModule.CreateSessionSecurityToken
中,默认的ValidTo属性设置为ValidFrom +默认令牌生存期。我可以看到设置SecurityToken.ValidTo的唯一方法是在创建安全令牌时。这是否意味着我需要实现一个自定义的SecurityToken类或者在那里
在WIF堆栈的某个地方,我可以拦截令牌的创建?到目前为止,我似乎只找到了以下事件处理程序FederatedAuthentication.SessionAuthenticationModule.SessionSecurityTokenCreated
,但此时已经创建了令牌,并且在那里我可以访问令牌,但正如预期的那样SecurityToken.ValidTo
属性是只是一个吸气剂。
同样,<microsoft.identityModel />
配置部分似乎没有此设置。有一个persistenLifeTime设置,但这仅适用于写入磁盘的cookie。
<microsoft.identityModel>
<federatedAuthentication>
<wsFederation
persistentCookiesOnPassiveRedirects="true" />
<cookieHandler
persistentSessionLifetime="60.0:0:0" />
</federatedAuthentication>
</microsoft.identityModel>
此外,为了使加密/解密与服务器无关,加密使用证书。为此,我以编程方式添加到联合提供程序的Global.asax中的会话安全令牌处理程序。我只是提到这一点,因为我想知道,如果我需要自定义SecurityToken.ValidTo
,我是否可能需要创建一个自定义安全令牌处理程序类,或者我现在是如何在Global.asax罚款和我需要到其他地方寻找解决SecurityToken.ValidTo
问题的方法吗?
<microsoft.identityModel>
<service>
<serviceCertificate>
<certificateReference x509FindType="FindByThumbprint" findValue="myThumbPrint" />
</serviceCertificate>
...
</microsoft.identityModel>
namespace MyCompany.IdentityServer.FederationProvider
{
public class Global : System.Web.HttpApplication
{
/// <summary>
/// Handles the Start event of the Application control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Application_Start(object sender, EventArgs e)
{
FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;
}
/// <summary>
/// Called when [service configuration created].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs"/> instance containing the event data.</param>
private void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
{
// The session security token handler needs to be overridden so that encryption/decryption is not server dependent via DPAPI.
// We need encryption/decryption to be server agnostic, so we make it certificate based instead.
// See http://blogs.msdn.com/b/distributedservices/archive/2012/10/29/wif-1-0-id1073-a-cryptographicexception-occurred-when-attempting-to-decrypt-the-cookie-using-the-protecteddata-api.aspx
// Use the <serviceCertificate> to protect the cookies that are
// sent to the client.
var sessionTransforms =
new List<CookieTransform>(new CookieTransform[] {
new DeflateCookieTransform(),
new RsaEncryptionCookieTransform(e.ServiceConfiguration.ServiceCertificate),
new RsaSignatureCookieTransform(e.ServiceConfiguration.ServiceCertificate) });
var sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
// This does nothing
//sessionHandler.TokenLifetime = someLifeTime;
e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
}
}
}
如果我创建了一个自定义的securityTokenHandler,我发现我可以指定一个生命周期,但这看起来就像我在上面的Global.asax中尝试的那样,sessionHandler.TokenLifetime = ...
<microsoft.identityModel>
<service>
<securityTokenHandlers>
<add type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel">
<sessionTokenRequirement lifetime="TimeSpan" />
</add>
</securityTokenHandlers>
...
</service>
</microsoft.identityModel>
我只能假设我遗漏了一些显而易见的东西,或者我是唯一可以自定义以获得我需要的SecurityToken.ValidTo
的行为?
答案 0 :(得分:2)
在STS中 - 设置SecurityTokenConfigurationConfiguration类的DefaultTokenLifetime属性以覆盖10h默认值。
答案 1 :(得分:0)
您可以使用此PowerShell脚本增加它
$sts = Get-SPSecurityTokenServiceConfig
$sts.FormsTokenLifeTime = (New-TimeSpan -minutes <NUMBER_OF_MINUTES>)
$sts.Update()
Get-SPSecurityTokenServiceConfig