【发布时间】:2019-09-25 13:12:46
【问题描述】:
我有一个具有经典客户端服务器架构的应用程序。 ASP.NET Core 位于后端和前端 jQuery、Angular 或 vanilla-js。
对于身份验证,我使用 OpenID-Connect 认证服务器。 (钥匙斗篷)
到目前为止,我可以通过 asp.net 核心身份验证中间件登录。获取我的访问令牌和刷新令牌。
我的配置如下:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.RequireHttpsMetadata = false;
options.MetadataAddress = Configuration["IdentityServer:Metadata"];
options.ClientId = Configuration["IdentityServer:ClientId"];
options.ClientSecret = Configuration["IdentityServer:ClientSecret"];
options.SaveTokens = true;
options.ResponseType = "code";
options.Scope.Add("openid profile email web origons roles SocpoeOnlyForDeveopers");
});
现在我想使用收到的访问令牌访问资源,不仅从我的后端,而且从我的前端。
到目前为止,我只找到了一种解决方案:将访问令牌存储在 _Layout.cshtml 中的 js 变量中。
当访问令牌过期时,我向后端发出请求以获取新的访问令牌。然后在后端,我用刷新令牌请求授权服务器获取新的访问令牌。然后我将访问令牌发送回前端。
因为我的访问令牌在 5 分钟后过期(缩短?)(默认为 keycloak),所以会有很多网络活动。
有没有更好的办法?
【问题讨论】:
标签: c# asp.net-core keycloak openid-connect asp.net-identity-3