【发布时间】:2015-09-02 07:33:12
【问题描述】:
我有一个使用 Web 表单编写的遗留应用程序。在这个项目中,我们开始将一些 web 表单转换为 SPA、angular.js 和 WebAPI。 SPA 页面直接与 WebAPI 通信。这个想法是,最终,所有的网络表单都将转换为新技术。
对于 SPA 页面,我实现了 adal.js,对于网络表单,我使用 ADAL.net。两者显然都在使用 Azure Active Directory。但是,他们似乎没有使用相同的不记名令牌,因为单点登录不起作用。从网络表单页面移动到 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.net 进行 Web 登录的吗? ADAL.net 不是为此而设计的,它旨在用于调用 Web API...
-
Vibronet,我很抱歉。我混淆了 OWIN 和 ADAL。我添加了上面的代码,所以你可以看到我在做什么。除了 Home.aspx 之外,所有 web 表单页面都通过不让匿名进入来保护。在 Home.aspx.cs 上,在预渲染中,如果用户未通过身份验证,我会发出 Owin 质询。我必须让 webforms 和 Adal.js 一起工作。有没有办法做到这一点?
-
没有问题。我想我们现在可以尝试回答了
标签: azure oauth azure-active-directory adal adal.js