SessionAuthenticationModule 类

定义

实现一个 在 ws-discovery 方案下处理会话 cookies 的 ASP.NET 模块。

public ref class SessionAuthenticationModule : System::IdentityModel::Services::HttpModuleBase
public class SessionAuthenticationModule : System.IdentityModel.Services.HttpModuleBase
type SessionAuthenticationModule = class
    inherit HttpModuleBase
Public Class SessionAuthenticationModule
Inherits HttpModuleBase
继承
SessionAuthenticationModule

示例

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup

    //SUBSCRIBE TO SAM EVENTS
    FederatedAuthentication.SessionAuthenticationModule.SessionSecurityTokenCreated += new EventHandler<SessionSecurityTokenCreatedEventArgs>(SessionAuthenticationModule_SessionSecurityTokenCreated);
    FederatedAuthentication.SessionAuthenticationModule.SessionSecurityTokenReceived += new EventHandler<SessionSecurityTokenReceivedEventArgs>(SessionAuthenticationModule_SessionSecurityTokenReceived);
    FederatedAuthentication.SessionAuthenticationModule.SigningOut += new EventHandler<SigningOutEventArgs>(SessionAuthenticationModule_SigningOut);
    FederatedAuthentication.SessionAuthenticationModule.SignedOut += new EventHandler(SessionAuthenticationModule_SignedOut);
    FederatedAuthentication.SessionAuthenticationModule.SignOutError += new EventHandler<ErrorEventArgs>(SessionAuthenticationModule_SignOutError);
}
void SessionAuthenticationModule_SignOutError(object sender, ErrorEventArgs e)
{
    System.Diagnostics.Trace.WriteLine("Handling SignOutError event");
}

void SessionAuthenticationModule_SignedOut(object sender, EventArgs e)
{
    System.Diagnostics.Trace.WriteLine("Handling SignedOut event");
}

void SessionAuthenticationModule_SigningOut(object sender, SigningOutEventArgs e)
{
    System.Diagnostics.Trace.WriteLine("Handling SigningOut event");
}

void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e)
{
    System.Diagnostics.Trace.WriteLine("Handling SessionSecurityTokenReceived event");
}

void SessionAuthenticationModule_SessionSecurityTokenCreated(object sender, SessionSecurityTokenCreatedEventArgs e)
{
    System.Diagnostics.Trace.WriteLine("Handling SessionSecurityTokenCreated event");
    //Store session on the server-side token cache instead writing the whole token to the cookie.
    //It may improve throughput but introduces server affinity that may affect scalability
    FederatedAuthentication.SessionAuthenticationModule.IsReferenceMode = true;
}

以下 XML 演示如何在 ASP.NET 管道中配置 SAM。 为简洁起见,此处省略了典型配置中存在的许多其他元素。

<configuration>  
  <system.webServer>  
    <modules>  
      <!--WIF 4.5 modules -->  
      <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>  
      <add name="WsFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>  
    </modules>  
  </system.webServer>  
</configuration>  

注解

当 ASP.NET 管道中存在时, SessionAuthenticationModule (SAM) 在WS-Federation方案中处理会话 Cookie。 它使用 属性 CookieHandler 指定的 Cookie 处理程序从 HTTP 请求中读取原始会话 Cookie 并将其写入 HTTP 响应。 它使用 SessionSecurityTokenHandler 为应用程序配置的 ,将原始会话 Cookie 反序列化为 SessionSecurityToken 对象。 会话安全令牌包含声明 (Claim) 和主体 (ClaimsPrincipal) ,这些声明与要为其处理请求的实体相关联。

SAM 将其 OnAuthenticateRequest 事件处理程序添加到 HttpApplication.AuthenticateRequest ASP.NET 管道中的 事件。 此处理程序将截获登录请求,如果存在会话 Cookie,则将其反序列化为会话令牌,并将 和 HttpContext.User 属性设置为Thread.CurrentPrincipal会话令牌中包含的声明主体。 在此过程中,它会调用 SAM 公开的其他几种方法。

SignOut可以调用 方法将用户从会话 (注销,例如,在 SignOut.aspx.cs 代码隐藏文件中) 。

SAM 公开多个事件,这些事件提供对其处理管道的访问权限。 和 SessionSecurityTokenReceivedSessionSecurityTokenCreated 事件使你能够修改从 Cookie 中读取或在处理期间创建的会话令牌。 通常,这样做是为了在令牌中添加、删除或转换声明,或者调整其过期时间。 SigningOutSignedOutSignOutError 事件为注销请求的处理提供挂钩。 在许多情况下,只需将这些事件的处理程序(通常添加到 global.asax.cs 文件)就足够了。

对于更复杂的方案,可以从 派生 SessionAuthenticationModule 来实现自定义 SAM。 为此,在 和 SignOut 期间OnAuthenticateRequest调用的许多方法都会公开,以便你可以在会话处理生命周期的特定阶段提供自定义行为。

通过将 SAM 添加到 IIS 版本 7 及更高版本的 元素下或 IIS 7 之前版本的 元素下的 <system.webServer><system.web> HTTP 模块,可以将 SAM 添加到配置文件中的 ASP.NET 管道。 SAM 使用的 Cookie 处理程序可以使用 cookieHandler> 元素进行配置<

构造函数

SessionAuthenticationModule()

初始化 SessionAuthenticationModule 类的新实例。

属性

ContextSessionSecurityToken

获取当前 SessionSecurityToken 的活动 HttpContext

CookieHandler

获取用于读取、写入和删除会话 Cookie 的 Cookie 处理程序。

FederationConfiguration

获取或设置对于当前模块有效的 FederationConfiguration 对象。

(继承自 HttpModuleBase)
IsReferenceMode

获取或设置一个值,该值指定是否应在会话 Cookie 中存储会话信息(声明值等),或是否应通过使用 Cookie 仅存储引用在服务器端上存储会话内容。

方法

AuthenticateSessionSecurityToken(SessionSecurityToken, Boolean)

通过验证传入会话标志请求该传入的身份验证。 成功验证后,它会使用指定的 SessionSecurityToken更新当前 HTTP 上下文和线程主体。

ContainsSessionTokenCookie(HttpCookieCollection)

确定指定的会话cookie是否在指定的cookie集合中。

CreateSessionSecurityToken(ClaimsPrincipal, String, DateTime, DateTime, Boolean)

使用配置会话标记处理程序,从指定参数创建 SessionSecurityToken

DeleteSessionTokenCookie()

从堆栈中移除最顶层会话并将其返回。

Dispose()

释放由 HttpModuleBase 类的当前实例占用的资源(内存除外)。

(继承自 HttpModuleBase)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
Init(HttpApplication)

初始化 HTTP 模块。

(继承自 HttpModuleBase)
InitializeModule(HttpApplication)

初始化模块,并准备它以处理来自模块 ASP.NET 应用程序对象的事件。

InitializePropertiesFromConfiguration()

初始化基于在配置文件中的定义的模块属性。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnAuthenticateRequest(Object, EventArgs)

AuthenticateRequest处理来自 ASP.NET 管道的事件。

OnPostAuthenticateRequest(Object, EventArgs)

PostAuthenticateRequest处理来自 ASP.NET 管道的事件。

OnSessionSecurityTokenCreated(SessionSecurityTokenCreatedEventArgs)

引发 SessionSecurityTokenCreated 事件。

OnSessionSecurityTokenReceived(SessionSecurityTokenReceivedEventArgs)

引发 SessionSecurityTokenReceived 事件。

OnSignedOut(EventArgs)

引发 SignedOut 事件。

OnSigningOut(SigningOutEventArgs)

引发 SigningOut 事件。

OnSignOutError(ErrorEventArgs)

引发 SignOutError 事件。

ReadSessionTokenFromCookie(Byte[])

从指定的会话 Cookie 读取 SessionSecurityToken

SetPrincipalFromSessionToken(SessionSecurityToken)

HttpContextThread 的主体设置到包含在指定会话标记的主体。

SignOut()

注销当前用户并引发相应事件。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
TryReadSessionTokenFromCookie(SessionSecurityToken)

尝试从一个会话 cookie 中读取一个 SessionSecurityToken,并返回指示此会话 cookie 是否成功读取的值。

ValidateSessionToken(SessionSecurityToken)

验证指定的 SessionSecurityToken 并返回其标识。

WriteSessionTokenToCookie(SessionSecurityToken)

将指定的 SessionSecurityToken 写入会话 cookie。

事件

SessionSecurityTokenCreated

当会话安全标记已创建时发生。

SessionSecurityTokenReceived

当会话安全标记已从 cookie 读取时发生。

SignedOut

在用户注销之后发生。

SigningOut

在删除登录会话之前发生。

SignOutError

当在注销期间有错误时发生。

适用于