2017-07-31 18 views
0

私の大学のWebサイトでShibboleth認証を実装しようとしています。大学は、Shibbolethの認証を使用し、それが.htaccessファイルに以下を追加することで有効になります。ユーザーは、大学のログインで認証する場合ヘッダーを使用した.NET Core 2 Shibboleth認証ミドルウェア

AuthType shibboleth 
ShibRequestSetting requireSession 1 
Require shib-session 

、shibユーザー属性は、ユーザーのアイデンティティを反映するように更新されます。私は、この情報を使って一般的なプリンシパルを生成し、データベースに格納されているそのユーザーに関連付けられたロールを割り当てます。私はミドルウェアに関する多くの情報をそこから見つけることができず、私はこれをどのように達成するのか分かりません。ありがとう!

答えて

0

AuthenticationHandlerと書いて、ミドルウェアをワイヤーアップするヘルパーメソッドを追加する必要があります。

ここで同じ

public static class ShibbolethDefaults 
{ 
    public const string AuthenticationType = "Shibboleth"; 
} 

public class ShibbolethAuthenticationOptions : AuthenticationOptions 
{ 
    /// <summary> 
    /// Creates an instance of API Key authentication options with default values. 
    /// </summary> 
    public ShibbolethAuthenticationOptions() 
     : base(ShibbolethDefaults.AuthenticationType) 
    { 
    } 
} 

public class ShibbolethAuthenticationHandler : AuthenticationHandler<ShibbolethAuthenticationOptions> 
{ 
    private readonly ILogger logger; 

    public ShibbolethAuthenticationHandler(ILogger logger) 
    { 
     this.logger = logger; 
    } 

    protected override async Task<Microsoft.Owin.Security.AuthenticationTicket> AuthenticateCoreAsync() 
    { 
     var properties = new AuthenticationProperties(); 
     // Find Shibboleth in default location 
     string Shibboleth = null; 
     //am not sure what header is to be checked for Shibboleth 
     string authorization = Request.Headers.Get("Authorization"); 
     if (!string.IsNullOrEmpty(authorization)) 
     { 
      if (authorization.StartsWith("Shibboleth ", StringComparison.OrdinalIgnoreCase)) 
      { 
       Shibboleth = authorization.Substring("Shibboleth ".Length).Trim(); 
      } 
      else 
      { 
       this.logger.WriteInformation("Authorization skipped."); 

       return new AuthenticationTicket(null, properties); 
      } 
     } 
     else 
     { 
      this.logger.WriteWarning("Authorization header not found"); 

      return new AuthenticationTicket(null, properties); 
     } 

     //here you can read from the headers and add each claims 

     var nameClaim = new Claim(ClaimTypes.Name, HttpContext.Current.Request.Headers["principalName"]); 
     var givenClaim = new Claim(ClaimTypes.GivenName, HttpContext.Current.Request.Headers["givenName"]); 
     var surnameClaim = new Claim(ClaimTypes.SurName, HttpContext.Current.Request.Headers["surname"]); 
     var emailClaim = new Claim(ClaimTypes.Email, HttpContext.Current.Request.Headers["email"]); 
     var allClaims = Enumerable.Concat(new Claim[] { nameClaim,givenClaim,surnameClaim,emailClaim }, Enumerable.Empty<Claim>()); 

     var identity = new ClaimsIdentity(allClaims, ShibbolethDefaults.AuthenticationType); 
     var principal = new ClaimsPrincipal(new ClaimsIdentity[] { identity }); 

     // resulting identity values go back to caller 
     return new AuthenticationTicket(identity, properties); 
    } 

} 

public class ShibbolethAuthenticationMiddleware : AuthenticationMiddleware<ShibbolethAuthenticationOptions> 
{ 
    private readonly ILogger logger; 

    public ShibbolethAuthenticationMiddleware(OwinMiddleware next, IAppBuilder app, ShibbolethAuthenticationOptions options) 
     : base(next, options) 
    { 
     this.logger = app.CreateLogger<AuthenticationHandler>(); 
    } 

    protected override AuthenticationHandler<ShibbolethAuthenticationOptions> CreateHandler() 
    { 
     return new ShibbolethAuthenticationHandler(logger); 
    } 
} 

public static class ShibbolethAuthenticationExtensions 
{ 
    public static IAppBuilder UseShibbolethAuthentication(this IAppBuilder app, ShibbolethAuthenticationOptions options = null) 
    { 
     if (app == null) 
     { 
      throw new ArgumentNullException("app"); 
     } 

     app.Use(typeof(ShibbolethAuthenticationMiddleware), app, options != null ? options : new ShibbolethAuthenticationOptions()); 
     app.UseStageMarker(PipelineStage.Authenticate); 
     return app; 
    } 
} 

のための擬似コードは、私は擬似コードを作成し、このSO postを参照してくださいです。

関連する問題