2

I have implemented OWIN authentication in my project.But when I try to build the project, it is showing some errors.

The methods which are showing errors :

 public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
    //code    
   }
  public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
  //code 
    }

The showing error is

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

How can I fix this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kesiya Abraham
  • 753
  • 3
  • 10
  • 24

2 Answers2

3

If you are not using an await operator in the implementation of the function then you can remove the async modifier.

However, once you remove it, the compiler will expect you to provide a return value for Task. You can obviously create a Task however you choose, but in this instance you might want to just return the following (which is discussed here):

public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    return Task.FromResult<object>(null);
}
musefan
  • 47,875
  • 21
  • 135
  • 185
1

async is not part of the signature. Just because you're overriding an async method doesn't mean that you have to mark your override as async.

So, if you don't have any awaits in your code, just remove the async mark.

public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
//code    
}
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
//code 
}
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448