我有一个使用 Web 表单编写的旧应用程序。在这个项目中,我们开始将一些 Webform 转换为 SPA、Angular.js 和 WebAPI。 SPA 页面直接与 WebAPI 通信。我们的想法是,最终所有网络表单都将转换为新技术。
对于 SPA 页面,我实现了 adal.js,对于 Web 表单,我使用 ADAL.net。显然两者都在使用 Azure Active Directory。但是,他们似乎没有使用相同的不记名 token ,因为单点登录不起作用。从 Web 表单页面移动到 SPA 页面需要再次登录。
如何让单点登录在项目中正常工作?
我的代码如下:
public void ConfigureAuth( IAppBuilder app )
{
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>( );
app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType );
app.UseCookieAuthentication( new CookieAuthenticationOptions( ) );
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
Authority = "https://login.microsoftonline.com/XXXXX.onmicrosoft.com",
PostLogoutRedirectUri = "https://XXXX:4432/gbl/Home.aspx",
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse( );
context.Response.Redirect( "/Error?message=" + context.Exception.Message );
return Task.FromResult( 0 );
},
SecurityTokenValidated = async n =>
{
var uniqueName = n.AuthenticationTicket.Identity.FindFirst( "unique_name" ).Value;
var userName = getUserNameFromUniqueName( uniqueName );
var claims = getRoleClaims( n.AuthenticationTicket.Identity ).ToList2( );
claims.Add( new Claim( "unique_name", uniqueName ) );
claims.Add( new Claim( ClaimTypes.Name, userName ) );
claims.Add( new Claim( ClaimTypes.UserData, "" ) );
var profileClaims = new ClaimsTransformer( ).GetTake2ProfileClaims( userName );
claims.AddRange( profileClaims );
var newIdentity = new ClaimsIdentity( n.AuthenticationTicket.Identity.AuthenticationType, "given_name", "roles" );
newIdentity.AddClaims( claims );
n.AuthenticationTicket = new AuthenticationTicket( newIdentity, n.AuthenticationTicket.Properties );
},
}
} );
}
最佳答案
ADAL JS 和 OpenId Connect 中间件并不是真正设计为协同工作的 - 您的应用程序在 Webforms 或 MVC 中实现这一事实并没有真正产生影响,问题是 ADAL JS 期望与后端交互调用通过 OAuth2 不记名 token 保护的 Web API,而 OpenId Connect 希望通过 cookie 保护完整的回发。有关两种不同方法的背景知识,请参阅 http://www.cloudidentity.com/blog/2014/04/22/authentication-protocols-web-ux-and-web-api/ 。我认为您必须决定是否要迁移到 SPA 基础设施,在这种情况下您可以使用 ADAL JS 和 OAuth2 中间件,但 Web 表单会有点尴尬(但仍然可能),或者如果您想坚持使用基于回发的设计并使用 OpenId Connect。
关于azure - 结合 ADAL.Net 和 ADAL.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32321076/