问题
I would like to use ASP.NET WebAPI 2, but with my own authentication mechanism. I have tried to remove all the code that comes with SPA template in VS 2013, as well as normal WebAPI template. If I remove all auth-related code from the project and don't configure OWIN in it's Startup.Auth, I always get an error, that OWIN middleware authentication is not setup and I can't get to any controller's action.
What is the correct way to implement a token based authentication in WebAPI 2 with own code, bypassing what's aready is in OWIN host .dll's, including it's requirements of configuration?
回答1:
You can use the blank Asp.Net template and add Web Api 2 as nuget packages, or remove unused stuff from the other starter templates.
回答2:
Similar to John answer but you can remove the OWIN when creating the WebAPI. Much cleaner approach.
To do that: Select "Change authentication"
then choose "No Authenciation"
Now your project wont have the OWIN stuff
Cheers Choco
回答3:
You can just comment out the following lines of code:
Startup.cs:
//[assembly: OwinStartup(typeof(Yourpoject.Startup))]
public void Configuration(IAppBuilder app)
{
//ConfigureAuth(app);
}
WebApiConfig.cs:
//// Web API configuration and services
//// Configure Web API to use only bearer token authentication.
//config.SuppressDefaultHostAuthentication();
//config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
来源:https://stackoverflow.com/questions/26981322/use-webapi-2-without-owin-authentication-middleware