在ASP.NET OWIN OpenIdConnect代码授权流中用基于令牌的身份验证替换Cookie
我们有一个用ASP.NET编写的web应用程序,它使用MVC为单页应用程序提供服务,使用web API进行ajax调用 身份验证使用Microsoft.Owin和OpenIdConnect与Azure AD进行授权。OAUTH流是服务器端代码授权。 然后在Startup.Auth.cs中 然后,使用ASP.net cookie在客户端应用程序和ASP.net服务器之间对连接到应用程序的最终用户进行身份验证我们希望改用基于令牌的方法。如果你感兴趣的话 我试着替换 Microsoft.Owin.Security.OAuth和Startup.cs中的Nuget软件包Microsoft.Owin.Security.Cookies 替换在ASP.NET OWIN OpenIdConnect代码授权流中用基于令牌的身份验证替换Cookie,asp.net,owin,openid-connect,azure-active-directory,owin-middleware,Asp.net,Owin,Openid Connect,Azure Active Directory,Owin Middleware,我们有一个用ASP.NET编写的web应用程序,它使用MVC为单页应用程序提供服务,使用web API进行ajax调用 身份验证使用Microsoft.Owin和OpenIdConnect与Azure AD进行授权。OAUTH流是服务器端代码授权。 然后在Startup.Auth.cs中 然后,使用ASP.net cookie在客户端应用程序和ASP.net服务器之间对连接到应用程序的最终用户进行身份验证我们希望改用基于令牌的方法。如果你感兴趣的话 我试着替换 Microsoft.Owin.Se
app.UseCookieAuthentication(cookieAuthenticationOptions)
byapp.useAuthBeareAuthentication(新的OAuthBeareAuthenticationOptions())代码>
在my AccountController中,我们将质询从HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType、CookieAuthenticationDefaults.AuthenticationType)
toHttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType,OAuthDefaults.AuthenticationType)代码>
问题是,使用Cookie时,当流完成时,设置的Cookie会在web请求响应中自动发送,同时重定向到我们指定的url。
我在哪里可以找到OWIN通过UseAuthBeareAuthentication生成的承载人(如果有)**,**我应该在何时何地将其发送回我的客户SPA
<强>注:< /强>可以找到我们正在尝试做的开源示例。
< P>我认为有两种方法供您考虑。
使用javascript库在单页应用程序中执行登录和令牌获取。那么您的后端就是一个纯粹的web API,并且可以使用OAuth承载中间件来验证请求。后端不知道有关用户登录的任何信息。我们有一个采用这种方法的好例子。如果后端也需要API调用,那么你也可以考虑OnBealFor流。我通常推荐这种方法
使用服务器中的OpenIDConnect中间件执行用户登录和令牌获取。您甚至可以完全省略CookieAuthenticationMiddleware
的使用(尽管我不是100%确定)。您可以在您提到的AuthorizationCodeReceived
通知中捕获令牌,并且您可以使用URL片段中的令牌重定向回SPA。您还可以通过某种途径将令牌(缓存在服务器上)传递到javascript。无论哪种情况,您都需要确保外部调用方无法访问您的令牌
要记住的是当令牌过期时如何刷新令牌。如果您使用#1,大部分内容将由图书馆为您处理。如果你使用#2,你必须自己管理它。Vittorio还有一篇好文章,可以帮助你使用#2:thx!除非我弄错了。Opt1,不适用100%隐式流量。我们有很多服务器逻辑,可以用我们的逻辑“连接”第三方api请求(office 365图形等),我们还有deamons。看起来OnBehalfOf是我们所需要的,但是Azure ADv2.0仍然不支持它,我们现在正尝试迁移到v2.0。。。在Opt2中,您建议我重用AzureAD和我的服务器之间发出的相同JWT,以便在我的客户端和服务器之间进行授权,对吗?事实上,我希望一个库可以让ASP.NET发出一个令牌,而不是一个普通的旧cookie。啊,我明白了。是的,还没有v2的海外建筑运营管理局,对不起。你对#2的描述是正确的。你的建议也很好。。。您可以使用AAD令牌来初始化您自己的令牌类型,而不是使用AAD令牌来授权与客户端和服务器的交互。有几个开源库可以帮助您生成JWT,您可以使用其中的一个#2只是让您不必这么做,包括随之而来的加密和密钥管理。
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
var cookieAuthenticationOptions = new CookieAuthenticationOptions()
{
CookieName = CookieName,
ExpireTimeSpan = TimeSpan.FromDays(30),
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
SlidingExpiration = true,
};
app.UseCookieAuthentication(cookieAuthenticationOptions);
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
AuthorizationCodeReceived = (context) =>
{
/*exchange authorization code for a token
stored on database to access API registered on AzureAD (using ADAL.NET) */
},
RedirectToIdentityProvider = (RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) =>
{
/* Set the redirects URI here*/
},
});
}
public class AccountController : Controller
{
public void SignIn(string signalrRef)
{
var authenticationProperties = /* Proper auth properties, redirect etc.*/
HttpContext.GetOwinContext()
.Authentication.Challenge(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
public void SignOut(string signalrRef)
{
var authenticationProperties = /* Proper auth properties, redirect etc.*/
HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}