gpt4 book ai didi

asp.net - 在 ASP.NET OWIN OpenIdConnect 代码授权流程中用基于 token 的身份验证替换 Cookie

转载 作者:行者123 更新时间:2023-12-02 20:58:10 27 4
gpt4 key购买 nike

我们有一个用 ASP.NET 编写的 Web 应用程序,它使用 MVC 为我们的单页应用程序提供服务,并使用 Web API 进行 ajax 调用。

身份验证使用 Microsoft.OwinOpenIdConnect 以及 Azure AD 进行授权。 OAUTH流程是服务器端代码授权。然后在 Startup.Auth.cs 中我们有

    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*/
},
});
}

单击登录时,我们导航到一个 url,其路由映射到以下 MVC Controller 的方法

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);
}

然后,使用 ASP.NET cookie 在我们的客户端应用程序和 ASP.net 服务器之间对连接到我们的应用程序的最终用户进行身份验证。 我们想改用基于 token 的方法。如果您有兴趣this is the reason .

我尝试更换Microsoft.Owin.Security.OAuth 和 Startup.cs 中的 Nuget 包 Microsoft.Owin.Security.Cookies替换

app.UseCookieAuthentication(cookieAuthenticationOptions); 通过 app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

在我的AccountController中,我们将挑战从HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType); 到 HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType,OAuthDefaults.AuthenticationType);

问题是,使用 Cookie,当流程完成并重定向到我们指定的 URL 时,set-cookie 会在 Web 请求响应中自动发送。在哪里可以找到 OWIN 使用 UseOAuthBearerAuthentication 生成的承载(如果有)**,**我应该在何时何地将其发送回我的客户端 SPA

注意:我们正在尝试做的事情的开源示例可以在 this github repository 中找到。 .

最佳答案

我认为您可以考虑两种方法。

  1. 使用 JavaScript 库在单页应用程序中执行登录和 token 获取。那么你的后端就是一个纯粹的Web API,并且可以只使用OAuth承载中间件来验证请求。后端不知道有关用户登录的任何信息。我​​们有一个采用这种方法的很好的示例 here 。如果您的后端也需要进行 API 调用,您也可以考虑 OnBehalfOf 流程。我通常推荐这种方法。
  2. 使用服务器中的 OpenIDConnect 中间件来执行用户登录和 token 获取。您甚至可以完全省略 CookieAuthenticationMiddleware 的使用(尽管我不能 100% 确定)。正如您提到的,您可以在 AuthorizationCodeReceived 通知中捕获 token ,并且可以使用 URL 片段中的 token 重定向回您的 SPA。您还可以有一些路由将 token (缓存在您的服务器上)传递给您的 JavaScript。无论哪种情况,您都需要确保外部调用者无法访问您的 token 。

需要记住的是 token 过期时如何刷新 token 。如果您使用#1,大部分内容将由库为您处理。如果您使用#2,则必须自己进行更多管理。

关于asp.net - 在 ASP.NET OWIN OpenIdConnect 代码授权流程中用基于 token 的身份验证替换 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40109510/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com