2017-06-16 90 views
0

我的情況的佈局如下。Azure AD在多個網站上使用相同的Toke登錄

我有兩個應用程序,一個將通過Azure廣告進行身份驗證,並且根據登錄的用戶將顯示第二個應用程序的鏈接。 在localhost中,它可以正常工作,第二個應用程序可以讀取第一個應用程序中使用的令牌並對用戶進行身份驗證。

但是,當我在我的DEV環境中發佈時,第二個應用程序無法讀取cookie,並且要驗證用戶,Azure頁面將再次顯示。

看來,在本地主機的cookie訪問沒有任何問題,但是當我從IIS上發佈有效的域這不會發生。

我的代碼:


public void ConfigureAuth(IAppBuilder app) 
{ 
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

    //app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
    app.UseCookieAuthentication(new CookieAuthenticationOptions() 
    { 
     CookieName = "NameOfMyApp", 
     CookieDomain = "mydomain.com" 
    }); 
    app.Use(async (context, next) => { await next.Invoke(); }); 

    app.UseOpenIdConnectAuthentication(this.CreateB2EOptions()); 
    app.Use(async (context, next) => { await next.Invoke(); }); 

} 

private OpenIdConnectAuthenticationOptions CreateB2EOptions() 
{ 
    return new OpenIdConnectAuthenticationOptions 
    { 
     Authority = string.Format(b2e.AadInstance, "common"), 
     ClientId = b2e.ClientId, 
     RedirectUri = sso.RedirectUri, 
     PostLogoutRedirectUri = sso.RedirectUri, 
     Notifications = new OpenIdConnectAuthenticationNotifications { AuthenticationFailed = this.AuthenticationFailed }, 
     TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, SaveSigninToken = true }, 
     AuthenticationMode = AuthenticationMode.Passive, 
     AuthenticationType = "OpenIdConnect-B2E" 
    }; 

} 
+0

請您澄清一下您的意思是「第二個應用程序可以讀取第一個應用程序中使用的令牌並對用戶進行身份驗證」。 ?如何在第二個應用程序中使用openid connect或使用自己的代碼實現這一目標? –

回答

0

我找到了解決辦法。 這兩個應用程序都會與聲明共享Cookie。 爲此,兩個應用程序需要在web.config中共享同一個機器鍵。

的web.config

<configuration> 
    <system.web> 
     <machineKey decryptionKey="DECRYPTIONKEY" validationKey="VALIDATION_KEY" /> 
    </system.web> 
    </configuration> 

ConfigureAuth


public void ConfigureAuth(IAppBuilder app) 
{ 
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

    app.UseCookieAuthentication(new CookieAuthenticationOptions() 
    { 
     CookieName = "NameOfMyApp", 
     CookieDomain = "mydomain.com", 
     CookiePath = "/" 
    });  
} 

這樣既將共享相同的cookie,如果第一個應用程序註銷,第二個也將需要重新登錄。