応用技術と運用について:
プログラミング言語: C#
フロントエンド側: Windows Presentation Foundation (WPF) - .NET Framework 4.6.1
バックエンド側: Windows Communication Foundation (WCF) - .NET Framework 4.6.1
選択したプロジェクトで必要なパッケージを復元し、 PM コンソールで次のコマンドを実行します。
Update-Package -reinstall
このアプリケーションでは、エンタープライズ アーキテクチャ標準で WCF サービスを使用して承認と認証を使用する方法を示します。
WCF
ASP.NET認証サービス
カスタム認証
HTTP クッキー
認可 PrincipalPermission 属性
スレッドの現在のプリンシパル
メッセージインターセプター
WCF サービス アプリケーションを作成します。
ASP.NET 認証サービスを再利用する AuthenticationService.svc を追加します。
ユーザーバリデータクラスを作成します。
Global.asax でカスタム認証を有効にします。
有効なユーザーの場合は Cookie を返します。
サービス構成を変更します。
SumOperation、ReadOperation、WriteOperation という 3 つの異なるメソッドを使用して ExecuteOperationsService.svc を作成します。
サービス メソッドを、承認されたアクセスのみの PrincipalPermission 属性で修飾します。
ExecuteOperationsService クラスを AspNetCompatibilityRequirements 属性で装飾します。
サーバー アプリケーションにインターセプターを実装します。
サービス アプリケーションのインターセプターに ID 設定コードを実装します。
サービス側のコードを変更して、名前の代わりに役割を含めます。
ユーザー名とロールの保存には暗号化チケットを使用します。
クライアント アプリケーションを作成し、両方のサービスへの参照を追加します。
クライアント アプリケーションにインターセプターを実装します。
認証サービス インスタンスを作成し、Login() メソッドを呼び出します。
Cookieを受信して保存します。
ExecuteOperationsService インスタンスを作成し、SumOperation、ReadOperation、および WriteOperation を呼び出します。
ExecuteOperationsService クライアントに Cookie を添付します。
1. ASP.NET 認証サービスを再利用する AuthenticationService.svc を追加します。
新しい WCF サービスを追加し、AuthenticationService.svc という名前を付けます。 ASP.NET 認証サービスを公開するので、関連ファイルを削除します。
AuthenticationService.cs を削除する
IAuthenticationService.csを削除する
<%@ ServiceHost Language="C#" Service="System.Web.ApplicationServices.AuthenticationService" Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
2. Global.asax でカスタム認証を有効にし、有効なユーザーの場合は Cookie を返します。
新しい項目 [Web] > [グローバル アプリケーション クラス]をプロジェクトに追加します。
protected void Application_Start(object sender, EventArgs e) { AuthenticationService.Authenticating += AuthenticationService_Authenticating; }
private void AuthenticationService_Authenticating(object sender, AuthenticatingEventArgs e) { stringroles = string.Empty; e.Authenticated = UserIsValid(e, ref ロール); e.AuthenticationIsComplete = true; if (e.Authenticated) { string encryptedValue = FormsAuthentication.Encrypt(CreateFormsAuthenticationTicket(e,roles)); OperationContext.Current.OutcomingMessageProperties[HttpResponseMessageProperty.Name] = SetSetCookieInResponseChannelHeaders(encryptedValue); } }
このメソッドは、認証イベント引数のカスタム資格情報オブジェクトからユーザー名とパスワードを抽出します。次に、UserValidator クラスを使用してユーザー名とパスワードを検証します。
Authenticated プロパティは、ユーザーが有効であるか無効であるかをそれぞれ true / false で表します。
3. 認証サービスと ASP.NET 互換性を有効にする
次に、web.config ファイルを変更して以下を含める必要があります。
認証サービスを有効にする
ASP.NET互換性を有効にする
<system.web.extensions> <スクリプト> <ウェブサービス> <authenticationService Enabled="true"/> </webサービス> </スクリプト> </system.web.extensions>
4. サーバー アプリケーションにインターセプターを実装します。
これは、サービス呼び出しを行う必要があるたびに、オペレーション コンテキストに Cookie を添付する上記のコードは面倒な作業であると思われるためです。 WCF インターセプターを使用して、これらのタスクをバックグラウンドに移動できます。
MSDN: WCF Data Services により、アプリケーションが要求メッセージをインターセプトできるようになり、操作にカスタム ロジックを追加できるようになります。このカスタム ロジックを使用して、受信メッセージ内のデータを検証できます。これを使用して、リクエストごとにカスタム認可ポリシーを挿入するなど、クエリリクエストの範囲をさらに制限することもできます。
このステップに含まれるアクティビティは次のとおりです。
サーバーの web.config 内にインターセプターの動作を追加します。
インターセプター動作クラスを作成します。 (プロジェクト内を参照)
IdentityMessageInspectorクラスを作成します。 (プロジェクト内を参照)
<システム.サービスモデル> <サービス> <サービス名="AuthenticationAndAuthorization.ExecuteOperationsService" behaviourConfiguration="InterceptorBehavior" /> </サービス> <行動> <サービスの動作> <行動> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </動作> <behavior name="InterceptorBehavior"> <serviceDebug includeExceptionDetailInFaults="true" /> <interceptorBehaviorExtension /> </動作> </サービス動作> </動作> <拡張機能> <behaviorExtensions> <add name="interceptorBehaviorExtension" type="AuthenticationAndAuthorization.Extensions.IDispatchMessageInspector.InterceptorBehaviorExtension, AuthenticationAndAuthorization, Version=1.0.0.0, Culture=neutral"/> </behaviorExtensions> </拡張機能> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
5. Cookie の暗号化。
当社の Cookie には、Fiddler などの HTTP 検査ユーティリティで簡単に読み取ることができる情報が含まれています。これにより、Cookie 情報がセキュリティ上の脅威にさらされやすくなります。
FormsAuthenticationTicket : System.Web.Security 名前空間は、便利なクラスを提供します。このクラス インスタンス内にユーザー名とロール情報を保存できます。チケット クラスは、有効期限と暗号化機能も提供します。
private FormsAuthenticationTicket CreateFormsAuthenticationTicket(AuthenticatingEventArgs e, string role) => new FormsAuthenticationTicket(1, e.UserName, DateTime.Now, DateTime.Now.AddHours(24), true,roles, FormsAuthentication.FormsCookiePath);
string encryptedValue = FormsAuthentication.Encrypt(CreateFormsAuthenticationTicket(e, ロール)); private FormsAuthenticationTicket GetDecryptTicket(string encryptedTicket) => FormsAuthentication.Decrypt(encryptedTicket);
private HttpResponseMessageProperty SetSetCookieInResponseChannelHeaders(string cookieValue) { HttpResponseMessageProperty 応答 = new HttpResponseMessageProperty(); response.Headers[HttpResponseHeader.SetCookie] = FormsAuthentication.FormsCookieName + "= + cookieValue; 応答を返します。 }
<認証モード="フォーム"> <forms slideExpiration="true" name="AuthCookie" protection="All" timeout="20"/> </認証> <machineKey decryption="AES" validation="SHA1" decryptionKey="1523F567EE75F7FB5AC0AC4D79E1D9F25430E3E2F1BCDD3370BCFC4EFC97A541" validationKey="33CBA563F26041EE5B5FE9581076C40618DCC1218F5F447634EDE8624508A129"/>
6. クライアント アプリケーションにインターセプターを実装します。
このステップに含まれるアクティビティは次のとおりです。
Client App.config 内にインターセプターの動作を追加する
インターセプター動作クラスを作成します。 (プロジェクト内を参照)
CookieMessageInspectorクラスを作成します。 (プロジェクト内を参照)
<行動> <エンドポイントの動作> <動作名 ="InterceptorBehavior"> <interceptorBehaviorExtension /> </動作> </endpointBehaviors> </動作>
<エンドポイント アドレス="http://localhost:8046/ExecuteOperationsService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IExecuteOperationsService" Contract="ExecuteOperationsServiceReference.IExecuteOperationsService" name="BasicHttpBinding_IExecuteOperationsService" BehaviorConfiguration="InterceptorBehavior"/>