I have in a controller the following code to allow in the local environment not to use a token to authenticate, while in production environment a JWT token is necessary for authentication:
The "else" part needs a required import to make it work. However, it is in gray color because Visual Studio detects it is an import that is not being used (which is true in the local environment, but when compiling for another environment it is necessary in order to work).
The problem is that sometimes we clean up imports and remove those that are not being used. As this import is detected that it is not being used (when in fact it is necessary), the process deletes it. That action generates compilation errors when compiling for production environment.
Is it a Visual Studio bug? Is there any way to avoid this, and to force that import to be used? Or another way to rewrite the "if DEBUG" statement so the code can detect the import is being used?
I believe it is intended and it is not a bug.
In any case, I believe you can also use an if DEBUG in the usings. I tried it locally and the Code Cleanup of Visual Studio Community didn't clear it this way:
#if DEBUG
#else
using Microsoft.AspNetCore.Authentication.JwtBearer;
#endif
However, using the fully qualified namespace as @StriplingWarrior suggested, must be the cleaner way of achieving what you want.
[Authorize(Policy = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationSchema)]