OnActionExecuting OnAuthorization 哪里进行获取token
时间: 2024-06-01 17:09:08 浏览: 18
在 ASP.NET Core 中,获取 token 的流程通常是在 `OnAuthorization` 方法中进行的。在这个方法中,你可以检查请求头中是否存在 token,并且验证 token 是否有效。如果 token 无效,则可以返回一个未授权的 HTTP 响应,否则就放行请求。
而 `OnActionExecuting` 方法通常用于在执行请求方法之前,进行一些操作,例如日志记录、参数验证等操作。在这个方法中获取 token 是不合适的,因为它并不涉及到授权的问题。
相关问题
OnAuthorization OnActionExecuting 如何调用第三方登录接口
在 ASP.NET Core 中,你可以使用 HttpClient 来调用第三方登录接口。通常情况下,你需要在 OnAuthorization 或 OnActionExecuting 方法中执行这个操作。
以下是一个简单的示例:
```csharp
public async Task OnActionExecutingAsync(ActionExecutingContext context)
{
// 获取当前用户信息
var user = context.HttpContext.User;
if (user.Identity.IsAuthenticated)
{
// 用户已经登录,不需要进行第三方登录
return;
}
// 构造请求参数
var parameters = new Dictionary<string, string>
{
{ "client_id", "your_client_id" },
{ "client_secret", "your_client_secret" },
{ "redirect_uri", "your_redirect_uri" },
{ "code", "code_from_third_party_login_provider" },
{ "grant_type", "authorization_code" }
};
// 创建 HttpClient
using var client = new HttpClient();
// 发送请求
var response = await client.PostAsync("https://third.party.login.provider/token", new FormUrlEncodedContent(parameters));
// 处理响应
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<ThirdPartyLoginTokenResponse>();
// 根据第三方登录响应,获取用户信息
var userInfoResponse = await client.GetAsync($"https://third.party.login.provider/userinfo?access_token={result.AccessToken}");
if (userInfoResponse.IsSuccessStatusCode)
{
var userInfo = await userInfoResponse.Content.ReadAsAsync<ThirdPartyLoginUserInfo>();
// 将用户信息保存到 HttpContext 中,以便后续使用
context.HttpContext.Items["UserInfo"] = userInfo;
}
}
else
{
context.Result = new UnauthorizedResult();
}
}
```
上述代码中,我们首先检查当前用户是否已经登录。如果用户已经登录,则不需要进行第三方登录。否则,我们构造请求参数,使用 HttpClient 发送请求,获取第三方登录响应,再根据响应获取用户信息,并将用户信息保存到 HttpContext 中,以便后续使用。如果出现错误,则返回 401 Unauthorized 状态码。
C# OnActionExecuting 跳过
在 C# 中,可以通过在 ActionFilterAttribute 中重写 OnActionExecuting 方法来实现在执行 Action 之前对请求进行拦截和处理。如果需要跳过该方法的执行,可以使用 SkipActionFilterAttribute 属性来标记该方法,如下所示:
```
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
var skipFilter = context.ActionDescriptor.EndpointMetadata.OfType<SkipActionFilterAttribute>().Any();
if (!skipFilter)
{
// 执行自定义逻辑
}
}
}
[AttributeUsage(AttributeTargets.Method)]
public class SkipActionFilterAttribute : Attribute
{
}
```
在上述代码中,MyActionFilterAttribute 继承了 ActionFilterAttribute,并重写了 OnActionExecuting 方法。在方法中,首先获取 SkipActionFilterAttribute 属性是否存在,如果存在,则跳过该方法的执行,否则执行自定义逻辑。
使用 SkipActionFilterAttribute 属性可以灵活控制方法的执行,方便开发者根据实际情况进行自定义处理。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)