首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WCF 身分驗證服務實作指南:自定義驗證與 Cookie 管理

     編輯:關於.NET

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 驗證。

自定義身分驗證 Cookie

處理 CreatingCookie 事件 (代碼清單 10-30),將 CustomCredential 寫入票據 UserData,自行加密建立 HttpCookie 並設 e.CookieIsSet = true。

作者:玄魂
出處:https://www.cnblogs.com/xuanhun/ (改編自《.NET 安全揭秘》)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved