authentication - 在我的 Web 应用程序 Controller 中每次 API 调用时请求访问 token 是否正确?

标签 authentication oauth-2.0 asp.net-core-mvc openid asp.net-core-webapi

我在后端有一个 Web API,在前端有一个 Web 应用程序和电话应用程序。 我在所有三个上使用 Auth0 进行 OAuth/OpenId 身份验证。 Web API 和 Web 应用程序都是用 ASP .Net Core 1.1 MVC 编写的。

每次我从 Web 应用程序调用 Web API 时,我都会向 Auth0 请求访问 token 。因此,在需要访问我的 API 的每个 Controller 操作中,我都:

var client = new HttpClient();
                var tokenResponse = await client.PostAsync(tokenEndpoint,
                    new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
                    {
                new KeyValuePair<string, string>("grant_type", "client_credentials"),                
                new KeyValuePair<string, string>("client_id", _auth0Settings.ClientId),
                new KeyValuePair<string, string>("client_secret", _auth0Settings.ClientSecret),
                new KeyValuePair<string, string>("audience", _auth0Settings.ApiIdentifier),
                    }));

这是正确的做法吗?或者我应该在 Web 应用程序启动时执行一次此操作,然后将访问 token 存储在可以从所有 Controller 调用的全局变量中?

最佳答案

您应该只调用一次并重复使用 token 。当 token 过期时,您应该使用刷新 token 请求新 token 。如果您不使用刷新 token ,您应该将用户重定向到登录页面以重新进行身份验证,或者在网络应用程序上具有某种自动重新身份验证机制。

关于authentication - 在我的 Web 应用程序 Controller 中每次 API 调用时请求访问 token 是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44173950/

相关文章:

php - 如何覆盖 Laravel 5.6 的默认登录机制?

javascript - jquery 认证

java - WSO2 身份服务器身份验证管理 API 身份验证失败

http - AWS API 网关 : Add header if response is Unauthorized

c# - .net 核心注入(inject)和解析服务

ios - iPhone应用程序拒绝17.2 : app requires users sign in with their Facebook accounts

python - SSIS 使用 OAuth2 执行 Python 脚本到 API 的处理任务 - 使用保存的 token 访问被拒绝的文件

c# - 使用本地帐户使用安全的 ASP Net 5 web api

webpack - 使用 webpack 时出现 Bootstrap popper 问题

c# - 将 Startup.cs 移动到类库(包)项目 - ASP.NET 5