I'm creating a simple authentication handler to be used with an ASP.NET Core Web API using the instructions of an article. The following code is a part of this work.
public class CustomAuthenticationHandler : AuthenticationHandler<BasicAuthenticationOptions>
{
//Constructors and fields
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
//Validation of bearer token
//No awaitable method calls
}
}
Although this HandleAuthenticateAsync() method is marked as async, it doesn't actually await anything inside the method body at all. So, my questions are,
- Do I really need to use a method decorated with async for this? Would it affect the application if I omitted the async keyword?
- Can I rearrange this method which returns a
Task<AuthenticateResult>in the following way without violating the best practices described in docs.
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
return new Task<AuthenticateResult>(() =>
{
//Token validation
});
}