2017-02-02 17 views
-1

更新:のMicrosoftのAzure Active Directory認証のログインURL

As suggested I changed my Startup.auth.cs to code below 

    public partial class Startup 
    { 
     private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; 
     private static string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"]; 
     private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; 
     private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"]; 
     private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"]; 

     public static readonly string Authority = aadInstance + tenantId; 

     // This is the resource ID of the AAD Graph API. We'll need this to request a token to call the Graph API. 
     string graphResourceId = "https://graph.windows.net"; 

     public void ConfigureAuth(IAppBuilder app) 
     { 
      ApplicationDbContext db = new ApplicationDbContext(); 

      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

      app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

      app.UseOpenIdConnectAuthentication(
       new OpenIdConnectAuthenticationOptions 
       { 
        ClientId = clientId, 
        Authority = Authority, 
        PostLogoutRedirectUri = postLogoutRedirectUri, 

        Notifications = new OpenIdConnectAuthenticationNotifications() 
        { 
         // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away. 
         AuthorizationCodeReceived = (context) => 
         { 
          var code = context.Code; 
          ClientCredential credential = new ClientCredential(clientId, appKey); 
          string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; 
          AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID)); 



           AuthenticationResult result = 
           authContext.AcquireTokenByAuthorizationCode(
            code, 
            new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), 
            credential, 
            graphResourceId); 


          return Task.FromResult(0); 
         }, 


         RedirectToIdentityProvider = context => 
         { 
          string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase; 
          string currentUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.Path; 
          context.ProtocolMessage.RedirectUri = currentUrl; 
          context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl; 
          return Task.FromResult(0); 
         } 
        } 
       }); 
     } 
    } 

そして今、私はこれがあります。

enter image description here

時々DbEntityValidationExceptionは私ApplicationDbContextで発生(ストアに使用しますADALTokenCache)


ユーザー認証のためにAzure ADOu Office 365を使用してWebアプリケーションをコーディングする場合は、ログイン後にアプリケーションキーを作成し、AzureのURLをアプリケーションにリダイレクトする必要があります。 このurlはweb.configで設定する必要がありますが、Azure ADはアプリケーションが送信するURIパラメータを無視し、開発URIではなくプロダクションURIにリダイレクトします。

私の開発URL: https://localhost:44315/ 生産URL: http://timesheet.tecnun.com.br/

アプリケーションは、web.configファイルを無視し、常に生産のURL

web.configファイルにリダイレクトします:

<appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    <add key="ida:ClientId" value="xxxxx" /> 
    <add key="ida:AADInstance" value="https://login.microsoftonline.com/" /> 
    <add key="ida:ClientSecret" value="xxxx" /> 
    <add key="ida:Domain" value="tecnun.com.br" /> 
    <add key="ida:TenantId" value="xxx" /> 
    <add key="ida:PostLogoutRedirectUri" value="https://localhost:44315/" /> 
    </appSettings> 

私の紺碧の構成: azure configuration

Id 2つの環境、開発、生産/現場で作業するのが好きです。しかし、私は2つのアプリケーションを作成せずにこれを行う方法を見つけることができません。

+0

どのようにURLを使用すると、リダイレクトされているに見えますか?あなたはそれらを正しく登録しましたか? – juunas

+0

これが本当に問題ない場合は、[Graph Explorer](https://graphexplorer.cloudapp.net/)でアプリのサービスプリンシパルを見つけて、そこにある返信URLを確認することをお勧めします。 – juunas

+0

URLはどのようなものですか?アプリケーションの設定方法詳細をご記入ください。 –

答えて

0

同じアプリケーションを異なるリダイレクトURLで動作させるには、WebアプリケーションがIDデータプロバイダにリダイレクトされる前に動的に変更することができます。ここで

はあなたの参照のためのサンプルコードです:

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions 
    { 
     ClientId = clientId, 
     Authority = authority, 

     Notifications = new OpenIdConnectAuthenticationNotifications 
     { 
      RedirectToIdentityProvider=context=> 
      {         
       string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase; 
       string currentUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.Path; 
       context.ProtocolMessage.RedirectUri = currentUrl; 
       context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl; 
       return Task.FromResult(0); 
      } 
     } 
    }); 
+0

私は実際にAuthorizationCodeReceivedイベントを持っています。それを取り除かなければならないのですか? –

+0

申し訳ありませんが、動作しませんでした。新しいエラーが発生しました: 相関ID:c1b2e720-2354-4d4c-be51-8f61d78c66a1 タイムスタンプ:2017-02-09 22:03:18Z AADSTS50011:応答アドレス 'https:// localhost:44315 /'が一致しませんアプリケーション用に構成された返信アドレス: '62e13278-b1d8-458e-8bbf-27a3118d21e4'詳細:指定なし –

+0

この問題を解決するには、エラーメッセージに記載されているこのリダイレクトURLをポータルに登録する必要があります。 –

関連する問題