WCF 身分驗證服務 (System.Web.ApplicationServices.AuthenticationService) 讓任何支援 SOAP 的客戶端 (含非 .NET) 共用 ASP.NET Membership 驗證,實現跨應用單一憑證登入。
新增 .svc 指向 AuthenticationService,若需自定義驗證邏輯,處理 Authenticating 事件 (代碼清單 10-28):
void AuthenticationService_Authenticating(object sender, AuthenticatingEventArgs e) {
if (e.UserName.Contains("@xuanhun.com"))
e.Authenticated = Membership.Providers["xuanhunSqlProvider"].ValidateUser(e.UserName, e.Password);
else if (e.UserName.Contains("@xuanbing.com"))
e.Authenticated = Membership.Providers["xuanbingSqlProvider"].ValidateUser(e.UserName, e.Password);
else
e.Authenticated = Membership.Provider.ValidateUser(e.UserName, e.Password);
e.AuthenticationIsComplete = true;
}
於 Global.asax Application_Start 註冊事件處理器。
system.web.extensions/scripting/webServices/authenticationService 設 enabled="true" requireSSL="true";system.serviceModel 定義 basicHttpBinding、Transport 安全、aspNetCompatibilityEnabled="true" (代碼清單 10-29)。並需啟用 Forms 驗證。
處理 CreatingCookie 事件 (代碼清單 10-30),將 CustomCredential 寫入票據 UserData,自行加密建立 HttpCookie 並設 e.CookieIsSet = true。
作者:玄魂
出處:https://www.cnblogs.com/xuanhun/ (改編自《.NET 安全揭秘》)