Use WebAPI 2 without OWIN authentication middleware

雨燕双飞 提交于 2019-12-06 02:51:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!