Add existing WEB.API project to NopCommerce

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hello,

I have an existing WEB API 2 project with JWT authentication.
how I merge my WEB API application so it's similar like Administration project.
the route will be something like localhost/api/[myApiRoute]
I know there's an alternative way using plugin, but I got a dead end implementing JWT and my customization handler.

I appreciate your help.
8 years ago
vantian wrote:
Hello,

I have an existing WEB API 2 project with JWT authentication.
how I merge my WEB API application so it's similar like Administration project.
the route will be something like localhost/api/[myApiRoute]
I know there's an alternative way using plugin, but I got a dead end implementing JWT and my customization handler.

I appreciate your help.


I think you  can add Nop.WebApi project like Nop.Web. I have created a web api project customize Nop.Web. From my experience I suggest you add a new project in the nop solution like nop.web. And merge your existing code there.
8 years ago
There is a plugin example add web api to your existing project.
https://www.nopcommerce.com/p/1563/aspnet-web-api-plugin-for-nopcommerce-with-source-code.aspx
May this plugin can help you.
8 years ago
@sohelcse10_duet so how did you route your application with Nop.Web? is there something I need to configure inside Nop.Web project?

@anik1991 yes, thank you. I'm still finding a way how to add it without plugin
8 years ago
vantian wrote:
@sohelcse10_duet so how did you route your application with Nop.Web? is there something I need to configure inside Nop.Web project?


Yes. configure when app start in the Global.asax file.  you can see my code snip below
 //for webapi
            GlobalConfiguration.Configure(WebApiConfig.Register);
            WebApiDependencyRegistrar.Register();
8 years ago
So I ended up by adding a Web API to NOP.WEB project.

The steps I needed to perform were:

1. Add Web API Packages
    (http://stackoverflow.com/questions/11990036/how-to-add-web-api-to-an-existing-asp-net-mvc-4-web-application-project)
2. Add OWIN Packages
    (http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/)
3. Configure the Startup.cs

now you can use [System.Web.Http.Authorize] attribute to authorize your API using OWIN Authentication.
I also add new folder called API and put all API files there (Controller, Models, Provider, Handlers, etc.) so it would  easy to maintain.

For API routing, I'm using Attribute Routing (http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2). you can add config.MapHttpAttributeRoutes() at Startup.cs
8 years ago
vantian wrote:
So I ended up by adding a Web API to NOP.WEB project.

The steps I needed to perform were:

1. Add Web API Packages
    (http://stackoverflow.com/questions/11990036/how-to-add-web-api-to-an-existing-asp-net-mvc-4-web-application-project)
2. Add OWIN Packages
    (http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/)
3. Configure the Startup.cs

now you can use [System.Web.Http.Authorize] attribute to authorize your API using OWIN Authentication.
I also add new folder called API and put all API files there (Controller, Models, Provider, Handlers, etc.) so it would  easy to maintain.

For API routing, I'm using Attribute Routing (http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2). you can add config.MapHttpAttributeRoutes() at Startup.cs


Thanks for informing...
8 years ago
I'm curious. How did you get your API calls to authorize with JWT?

I've added the required nuget packages according to http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/ but when I call the secured API controller I keep getting the HTML for the login view as response instead of a "normal" 401.

I can call things that are not secured with the [Authorize] attribute, so the API it self is working.
I just can't seem to "skip" forms authentication in favor of token auth.

Any clues? Any information or nudges in the right direction will be much appreciated.

Thanks in advance,
Ludvig
8 years ago
By the way, you may also be interested in the following YouTube video by nop-templates (from nopdevdays conference) - https://www.youtube.com/watch?v=w2gWlS4AO4I
8 years ago
I take it the plugin wasn't ready in 2 months then since it is not in 3.7 :)
Really interesting video tho! I'll download the plugin and check it out.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.