2017-06-16 89 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,如果第一个应用程序注销,第二个也将需要重新登录。