简体   繁体   English

如何在ASP.NET Web API中处理持有者令牌的缺失或到期

[英]How to handle the absence or expiration of bearer token in ASP.NET Web API

I have an ASP.NET web API and I want to return a meaningful message instead of this exception in case if the token was expired or it wasn't provided in the request at all. 我有一个ASP.NET Web API,我希望返回一个有意义的消息而不是此异常,以防令牌过期或根本没有在请求中提供。 This is the exception that I'm getting in this case. 这是我在这种情况下得到的例外。

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "An error occurred when trying to create a controller of type 'SomeController'. Make sure that the controller has a parameterless public constructor.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Object reference not set to an instance of an object.",
        "ExceptionType": "System.NullReferenceException",
        "StackTrace": "   at WebApi.Controllers.SomeController..ctor() in SomePath\\Controllers\\SomeController.cs:line 21\r\n   at lambda_method(Closure )\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
    }
}

I have tried like this and added a breakpoint, but it doesn't even reach it, so I'm not sure where should I write the code to handle this. 我试过这样并添加了一个断点,但它甚至没有达到它,所以我不知道我应该在哪里编写代码来处理它。

public override void OnAuthorization(HttpActionContext actionContext)
{
    JsonHelper jsonHelper = new JsonHelper();
    if (actionContext == null)
    {
        actionContext.Response = jsonHelper.setHttpResponseMessage(jsonHelper.setResponse(false, null, "Expired or Incorrect token."));
        return;
    }
}

Thanks in advance 提前致谢

Thanks to @Mark Wagoner comment, I saw this sentence in the exception which I didn't notice before 感谢@Mark Wagoner的评论,我在例外中看到了这句话,我之前没有注意到

in SomePath\\Controllers\\SomeController.cs:line 21 在SomePath \\ Controllers \\ SomeController.cs中:第21行

In the controller in line 21, there is this line: 在第21行的控制器中,有这一行:

    ...    
    string variable = ClaimsPrincipal.Current.FindFirst("SomeValue").Value;

    public HttpResponseMessage Get()
    {
    ...

And since the token wasn't provided here, then it's to be expected that the above line would through Object reference not set to an instance of an object exception. 并且由于此处未提供令牌,因此可以预期上述行将通过Object引用而不设置为对象异常的实例 I declared it outside the method because it is used in multiple methods. 我在方法之外声明它,因为它用于多种方法。 So, I removed it from there & declared it for each method and that solved my problem. 所以,我从那里删除它并为每个方法声明它,这解决了我的问题。

The thing that I didn't know is that the variables that I declare in the controller outside of the methods are declared first, then it starts executing the code in custom authorize CustomAuthorizeAttribute : AuthorizeAttribute class. 我不知道的事情是我首先声明我在方法之外的控制器中声明的变量,然后它开始执行自定义授权CustomAuthorizeAttribute : AuthorizeAttribute类中的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用两个asp.net web api的持票令牌 - How to use bearer token with two asp.net web api 如何通过桌面/控制台应用程序使用带有不记名令牌的 ASP.NET Web API 2.0 - How to consume ASP.NET Web API 2.0 with bearer token via a desktop/console application 如何从HttpresponseMessage Asp.net Web Api返回承载令牌 - How to return bearer token from HttpresponseMessage Asp.net Web Api 在ASP.net身份中向Asp.Net Web API 2承载令牌添加声明? - Add Claims to Asp.Net Web API 2 Bearer Token in ASP.net Identity? asp net web api身份验证令牌到期? - asp net web api Authentication token expiration? 使用RestSharp,如何使用oAuth2 Bearer令牌执行对ASP.NET Web API的POST请求? - Using RestSharp, how do I execute a POST request to my ASP.NET Web API with an oAuth2 Bearer token? 我的 ASP.NET 核心应用服务 Web API 如何支持 AAD 承载令牌和客户端证书身份验证? - How can my ASP.NET Core App Service Web API support both AAD Bearer Token and Client Certificate Auth? 使用Microsoft Graph令牌通过Jwt Bearer令牌保护ASP.NET Core Web API - Using Microsoft Graph token to secure ASP.NET Core Web API with Jwt Bearer tokens Web api asp.net Ajax请求Bearer令牌登录在Fiddler中有效,但在Ajax调用中无效 - Web api asp.net ajax request for Bearer token login work in fiddler but does not work in ajax call 如何实现允许承载令牌身份验证或 api 密钥身份验证的 ASP.Net Core API Controller - How to implement an ASP.Net Core API Controller which allows Bearer Token authentication OR api key authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM