【发布时间】:2018-03-24 15:12:19
【问题描述】:
问题 后端网络应用程序是否可以从通过登录前台生成的 AAD 身份验证 cookie 接受并获取用户身份-end-web-app?
背景我想为 JavaScript SignalR 客户端实现 Azure Active Directory 单点登录体验,但我当前的解决方案要求用户登录两次。一次访问托管 js-client 的 Web 应用程序,然后再次访问 js-client-app 可以访问后端。
所需的解决方案与 SSO AAD 身份验证
- 前端(OWIN/MVC/JavaScript)
- 用于识别用户的 AAD 身份验证 - 已实现
- JS SignalR 客户端在服务器请求中包含 auth cookie - 默认工作
- 后端(OWIN/SignalR PersistentConnection)
- 通过 SignalR 请求中包含的前端身份验证中的加密 .AspNetCookies cookie 识别用户。
- cookie 未被接受,OWIN 的调试输出为 Microsoft.Owin.Security.Cookies.CookieAuthenticationMiddleware 警告:0 : Unprotect ticket failed
尝试过的后端 Startup.cs
public void ConfigureAuth( IAppBuilder app )
app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
TokenValidationParameters = new TokenValidationParameters
{
RoleClaimType = "roles"
}
});
}
当前解决方案出现不受欢迎的双重登录
- 前端网络应用程序(OWIN/MVC/JavaScript)
- Owin 处理 AAD 身份验证
- MVC根据经过身份验证的用户生成视图
- ADAL JS 在客户端处理用户身份验证并获取作为 cookie (my_access_token) 添加的访问令牌
- 后端网络应用程序(OWIN/SignalR PersistentConnection)
- OWIN 通过提取在 my-_access_token cookie 中找到的访问令牌并将其添加为不记名授权标头来处理身份验证
- 通过覆盖PersistentConnection中的Authorize函数来处理授权基于经过身份验证的用户
当前前端 Startup.cs
public void ConfigureAuth( IAppBuilder app )
GlobalFilters.Filters.Add( new AuthorizeAttribute() );
app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
TokenValidationParameters = new TokenValidationParameters
{
RoleClaimType = "roles"
}
});
}
当前后端 Startup.cs
public void Configuration( IAppBuilder app ){
app.Use( ( context, next ) =>
{
if (context.Request.Cookies.Any(c => c.Key.Equals("BearerToken")))
{
var cookie = context.Request.Cookies.First(c => c.Key.Equals("BearerToken"));
context.Request.Headers.Add("Authorization", new[] { $"Bearer {cookie.Value}" });
}
return next.Invoke();
});
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = tenant,
TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = audience,
RoleClaimType = "roles"
}
};
);
}
【问题讨论】:
标签: asp.net-mvc azure signalr owin-middleware azure-authentication