SessionAuthenticationModule 類別

定義

實作 ASP.NET 模組,它會處理在 WS-Federation 案例中的工作階段 cookie。

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 新增至組態檔中的 ASP.NET 管線,方法是將 SAM 新增至 IIS 第 7 版和更新版本之 元素底下的 <system.webServer> HTTP 模組,或在 IIS 7 之前的 版本底下 <system.web> 。 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()

刪除工作階段 Cookie,並從快取中將其移除。

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

當登出期間發生錯誤時發生。

適用於