2016-05-20 14 views
1

forms authenticationまたはwindows authenticationの代わりにカスタム認証サービスを呼び出す必要があります。このサービスは、認証が成功した場合にユーザーに関する情報を含むJSONオブジェクトを返します。MVC5:カスタム認証

これをASP.NET MVC 5にどのように統合できますか?

例:ASP.NETセキュリティパイプラインの認証手順を単純にオーバーライドし、引き続き認証パーツを使用する方法はありますか。どうしたらいい?

+1

をあなたはASP.netアイデンティティで見たことがありますか? – TryingToImprove

+2

サンプルプロジェクトのStartup.csを見ると、MVC5でシングルサインオンを追加するのは比較的簡単です。デモでは、facebook、twitter、およびGoogle認証を追加するためのコードをコメントアウトしているかもしれません。 – SamGhatak

答えて

0

私は認証なしで新しいMVCアプリケーションを作成しました。それから私は、web.configファイルにこのモジュールを登録するとASP.NET要求パイプライン(https://msdn.microsoft.com/en-us/library/bb470252.aspx

public class MyAuthModule : IHttpModule 
{ 
    public void Dispose() 
    { 
     // Implement when you need to release unmanaged resources 
    } 

    public void Init(HttpApplication context) 
    { 
     context.AuthenticateRequest += Context_AuthenticateRequest; 
     context.PostAuthenticateRequest += Context_PostAuthenticateRequest; 
    } 

    private void Context_AuthenticateRequest(object sender, EventArgs e) 
    { 
     // Call custom library to authenticate 
     // and create a new ClaimsPrincipal with the info from the JSON object. 
    } 

    private void Context_PostAuthenticateRequest(object sender, EventArgs e) 
    { 
     // Optional: Inspect the ClaimsPrincipal and add more claims 
    } 
} 

にフックカスタムモジュールを作成:

<system.webServer> 
    <modules> 
    <add name="MyAuthModule" 
     type="MvcDemo.Auth.MyAuthModule, MvcDemo" /> 
    </modules> 
</system.webServer>