2017-12-05 26 views
1

GitHubに次のアプリケーションがあり、Azure Appサービスのhttps://stratml.servicesにMicrosoft Accountとして定義されている認証を使用し、Microsoftアカウントのサインインを要求する任意の要求で展開しました。 "挑戦する"このチャレンジが発生しますが、https://stratml.services/Home/IdentityNameはコンテンツを返しません。ASP.NET Core 2とAzure App ServicesでのMicrosoft認証

私はthisthisに従っていますが、私はEntityFrameworkを使用したくありません。後者の説明からは、私の認証方式を正しく構成するかどうかはわかりません。

この次のコードは、私のスタートクラスである:

 services.AddAuthentication(options => 
     { 
      options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      options.DefaultChallengeScheme = MicrosoftAccountDefaults.AuthenticationScheme; 
     }).AddMicrosoftAccount(microsoftOptions => 
     { 
      microsoftOptions.ClientId = Configuration["Authentication:AppId"]; 
      microsoftOptions.ClientSecret = Configuration["Authentication:Key"]; 
      microsoftOptions.CallbackPath = new PathString("/.auth/login/microsoftaccount/callback"); 

     }); 

アップデート:私は得ることができた最初の答えのおかげで、それは今、私のアプリケーションへのフィードバックにMicrosoftとの試みに許可しかし、私は次のことを受け取りますエラー:

InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: Cookies 

https://stratml.services/Home/IdentityNameを訪問し、GitHubのは、更新されています。 、ConfigureServices方法では

をクッキーとMSAの認証サービスを追加します。

 services.AddAuthentication(options => 
     { 
      options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      options.DefaultChallengeScheme = MicrosoftAccountDefaults.AuthenticationScheme; 
     }).AddCookie(option => 
     { 
      option.Cookie.Name = ".myAuth"; //optional setting 
     }).AddMicrosoftAccount(microsoftOptions => 
     { 
      microsoftOptions.ClientId = Configuration["Authentication:AppId"]; 
      microsoftOptions.ClientSecret = Configuration["Authentication:Key"]; 

     }); 

答えて

1

私は私のテストに基づいて、私の側にこの問題をチェックして、あなたは次のように設定をconfgureことができます。 Configure法の下では

services.AddAuthentication(options => 
{ 
    options.DefaultChallengeScheme = MicrosoftAccountDefaults.AuthenticationScheme; 
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
}) 
.AddCookie(option => 
{ 
    option.Cookie.Name = ".myAuth"; //optional setting 
}) 
.AddMicrosoftAccount(microsoftOptions => 
{ 
    microsoftOptions.ClientId = Configuration["Authentication:AppId"]; 
    microsoftOptions.ClientSecret = Configuration["Authentication:Key"]; 
}); 

app.UseAuthentication()を追加します。

TEST:私はあなたのオンラインのウェブサイトをチェックするとき

[Authorize] 
public IActionResult Index() 
{ 
    return Content(this.User.Identity.Name); 
} 

enter image description here

、私はあなたがAuthentication and authorization in Azure App ServiceAuthenticate with Microsoft accountを使用していることがわかりました。

enter image description here

アプリサービスの認証を使用した場合、クレームは現在のユーザーに添付することができなかった私の知る限り、あなたはRequest.Headers["X-MS-CLIENT-PRINCIPAL-NAME"]を経由してのアイデンティティ名を取得することができます。また、手動で現在のためのすべての請求を添付するために、この同様のissueをたどることができユーザー。

一般に、アプリケーションで認証ミドルウェアを手動で有効にするか、認証を有効にするコードを変更せずにAzureによって提供されるアプリサービス認証を利用することができます。さらに、Remote debugging web appsでアプリケーションのトラブルシューティングを行うこともできます。

UPDATE:

私のコードでのMSAの認証を有効にし、紺碧に展開するとき、それをテストするために、私はその後、アプリケーションサービスの認証無効紺碧のウェブアプリに自分のアプリケーションを導入しました。私は新しいシークレットウィンドウを開き、私のWebアプリケーションが期待どおりに動作することがわかった。

enter image description here

ローカルにMSAログインをシミュレートし、紺碧に配置されたとき簡単に認証を使用する場合、私はあなたがappsettings.jsonで設定値を設定し、手動でDEVとオーバーライドの認証ミドルウェアを追加することができると仮定紺碧の設定、あなたが従うことができる詳細here。そして、あなたは、同じアプリケーションIDを使用してリダイレクトURLを以下のように設定できます。

https://stratml.services/.auth/login/microsoftaccount/callback //for easy auth 
https://localhost:44337/signin-microsoft //manually MSA authentication for dev locally 

また、あなたはこのissueを手動で現在のユーザーのすべての請求を添付する従うことができます。その後、手動でのMSA認証とEasy Authの場合と同様に、ユーザークレームを取得できます。

+0

私は本当にコールバックURLの仕組みを理解していませんが、Azureは有効なものを提供しましたが、私が提供したものが有効ではないというエラーが表示されます。そこに何らかの着陸を作り出す必要がありますか、または追加の配線を行う必要がありますか?しかし、これは私が望むものに非常に近いです。私がこのプロジェクトでAzureを好きになるほど、私はそれにコード依存を望んでいません。 –

+0

私はちょうどいくつかのテストについて私の答えを更新しました、あなたはそれを参照することができます。 –

0

は、Microsoftのドキュメントpageに応じて、アプリケーションサービスの認証(EasyAuth)を使用している場合:

App Service passes some user information to your application by using special headers. External requests prohibit these headers and will only be present if set by App Service Authentication/Authorization. Some example headers include:

X-MS-CLIENT-PRINCIPAL-NAME

X-MS-CLIENT-PRINCIPAL-ID

X-MS-TOKEN-FACEBOOK-ACCESS-TOKEN

X-MS-TOKEN-FACEBOOK-EXPIRES-ON

Code that is written in any language or framework can get the information that it needs from these headers. For ASP.NET 4.6 apps, the ClaimsPrincipal is automatically set with the appropriate values.

あなたはASP.NETコア2.0を使用しているのであれば基本的には、手動でClaimPrincipalを設定する必要があります。あなたはこのヘッダをフェッチし、ClaimsPrincipalあなたはその後、ミドルウェア

public static class AppServiceAuthExtensions 
{ 
    public static AuthenticationBuilder AddAppServiceAuthentication(this AuthenticationBuilder builder, Action<AppServiceAuthenticationOptions> configureOptions) 
    { 
     return builder.AddScheme<AppServiceAuthenticationOptions, AppServiceAuthenticationHandler>("AppServiceAuth", "Azure App Service EasyAuth", configureOptions); 
    } 
} 

ための拡張メソッドを書いてConfigure()方法とプットでapp.UseAuthentication();を追加することができますAuthenticationHandler

public class AppServiceAuthenticationOptions : AuthenticationSchemeOptions 
{ 
    public AppServiceAuthenticationOptions() 
    { 
    } 
} 

internal class AppServiceAuthenticationHandler : AuthenticationHandler<AppServiceAuthenticationOptions> 
{ 
    public AppServiceAuthenticationHandler(
     IOptionsMonitor<AppServiceAuthenticationOptions> options, 
     ILoggerFactory logger, 
     UrlEncoder encoder, 
     ISystemClock clock) : base(options, logger, encoder, clock) 
    { 
    } 

    protected override Task<AuthenticateResult> HandleAuthenticateAsync() 
    { 
     return Task.FromResult(FetchAuthDetailsFromHeaders()); 
    } 

    private AuthenticateResult FetchAuthDetailsFromHeaders() 
    { 
     Logger.LogInformation("starting authentication handler for app service authentication"); 

     if (Context.User == null || Context.User.Identity == null || Context.User.Identity.IsAuthenticated == false) 
     { 
      Logger.LogDebug("identity not found, attempting to fetch from the request headers"); 

      if (Context.Request.Headers.ContainsKey("X-MS-CLIENT-PRINCIPAL-ID")) 
      { 
       var headerId = Context.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"][0]; 
       var headerName = Context.Request.Headers["X-MS-CLIENT-PRINCIPAL-NAME"][0]; 

       var claims = new Claim[] { 
        new Claim("http://schemas.microsoft.com/identity/claims/objectidentifier", headerId), 
        new Claim("name", headerName) 
       }; 
       Logger.LogDebug($"Populating claims with id: {headerId} | name: {headerName}"); 

       var identity = new GenericIdentity(headerName); 
       identity.AddClaims(claims); 

       var principal = new GenericPrincipal(identity, null); 
       var ticket = new AuthenticationTicket(principal, 
        new AuthenticationProperties(), 
        Scheme.Name); 

       Context.User = principal; 
       return AuthenticateResult.Success(ticket); 
      } 
      else 
      { 
       return AuthenticateResult.Fail("Could not found the X-MS-CLIENT-PRINCIPAL-ID key in the headers"); 
      } 
     } 

     Logger.LogInformation("identity already set, skipping middleware"); 
     return AuthenticateResult.NoResult(); 
    } 
} 

で設定するために使用する必要がありますどのようなスタートアップクラスのConfigureServices()メソッドに従います。あなたは完全な主張の詳細が必要な場合は

services 
    .AddAuthentication(options => 
    { 
     options.DefaultAuthenticateScheme = "AppServiceAuth"; 
     options.DefaultChallengeScheme = "AppServiceAuth"; 
    }) 
    .AddAppServiceAuthentication(o => { }); 

、あなたは/.auth/meに要求を行うことにより、AuthenticationHandlerでそれを取得し、あなたがリクエストで受信したのと同じクッキーを使用することができます。

+0

これはMicrosoftAccountを置き換えますか?これをローカルにシミュレートする蟻の方法はありますか? –

+0

はい、App Service EasyAuthを使用しているため、Microsoftアカウント認証方式をこれに置き換える必要があります。 AFAIK、現時点ではこれをローカルでシミュレートする方法はありません。一方、 'AuthenticationHandler'がチェックをスキップして、' AuthenticationResult.NoResult() 'を直ちに返すように設定することができます。環境は開発です。 – Hendry

関連する問題