How To Add Custom ModelValidatorProviders To Web API Project? - c#

I'm moving some MVC code to Web API and I need to update my custom ModelValidatorProviders. It seems as though my validators can stay much the same only they should inherit the System.Web.Http.Validation namespace.
What I can't figure out is how to add the provider to Web API. When using MVC I can just add the following to my global.asax:
ModelValidatorProviders.Providers.Add(new CustomModelValidatorProvider());
How do I use the custom provider with Web API?

This page Configuring ASP.NET Web API helped me answer my own question. Specifically this is what I ended up doing:
GlobalConfiguration.Configuration.Services.Add(typeof(ModelValidatorProvider), new CustomModelValidatorProvider());

Related

Which controllers type should I use in asp.net web api?

I am creating an asp.net web api project, I am storing my data in mssql server db, and I am using an Entity Framework. I was following the microsoft guide (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio), it uses an api controller with entity framework scaffold item. In other guides I found people using controllers derived from ApiController class and the routing there is made via web.config file. What is the actual difference, and what should I use? I suppose I should follow the MC guide cause it is fairly new (was added a week ago).
P.S.: right now, I am not planning to add "view" part to my app, first I want to create all the API calls, but later I whould like to.
Asp.net6 brought new ways of working with controllers, detaching a little from the MVC pattern we had, but this model of yours, if you are following the tutorial, ends up inheriting from the MVC standard ApiController, look at this part of the official tutorial:
Marks the class with the [ApiController] attribute. This attribute
indicates that the controller responds to web API requests. For
information about specific behaviors that the attribute enables, see
Create web APIs with ASP.NET Core. Uses DI to inject the database
context (TodoContext) into the controller. The database context is
used in each of the CRUD methods in the controller. The ASP.NET Core
templates for:
Controllers with views include [action] in the route template. API
controllers don't include [action] in the route template.
well this implies that your controller is being mapped into the aspnet mvc stream and being registered there when you call app.MapControllers(); in your Program.cs
and it's not necessary to configure anything in webConfig, which doesn't even exist anymore since the framework
You can follow the tutorial and everything will be fine.

How to add a custom JWT engine to a .NET 6.0 project

Admittedly, I am new to creating RESTful services. I have created a new ASP.NET Core Web API project. I have the main part of the REST service created, but I need to add authentication. For this app, I cannot use Microsoft's Identity package.
I have found a JWT Authentication article that seems to work well for me, but it's implemented as a console program--there's no information on how to wire it into a Web API project (facepalm). JWT Authentication using C#
I have done a lot of looking on the net, but I can't seem to find what I need.
Any suggestions will be appreciated.

How to crete views (forms for user) in Entity Framework

I would like to write application in asp with login system.
I found this tutorial tutorial. I read it but I don't know how can I create forms (pages) for user, where he can write something. Could Somebody tell me how can I do it?
You can create a basic form using WebForms however I would preffer to use MVC because it give more control and flexibility in designing the user interface.
The tutorial you found shows you how to use identity with a web api. The web api will provide you a way to manipulate data. The data should be consumed by either a webform or mvc application. Here is a tutorial for how to make an MVC application.
http://www.asp.net/mvc/overview/getting-started/introduction/getting-started
This is a link to a video course that helped me learn MVC and Web API.
https://mva.microsoft.com/en-US/training-courses/developing-asp-net-mvc-4-web-applications-jump-start-8239?l=fwYCcoJy_2404984382

Add existing WEB.API project to NopCommerce

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.
So I ended up by adding a Web API to NOP.WEB project.
The steps I needed to perform were:
Add Web API Packages
(How to add Web API to an existing ASP.NET MVC 4 Web Application project?)
Add OWIN Packages
(http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/)
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

AuthoriseAttribute with Web API in existing MVC Application

I have an existing MVC app with a custom authorize attribute overriding System.Web.Mvc.AuthorizeAttribute.
I now need to add Web API to the project, but only allow access to authorized users.
If I add my custom authorize attribute to the API controller, it seems to be getting ignored and allows anyone unrestricted access.
After a bit of reading, I have found that to authorize users for Web API, you have to use System.Web.Http.AuthorizeAttribute version of the authorise attribute.
However, after adding the Http version of the authorize attribute to my API controller, and keeping the Mvc version of the authorise attribute to my Mvc controllers, my requests to the API are now always returning 401 - Unauthorised, even when logged in.
I then tried removing my custom [Mvc] authorise attribute and use the standard version instead and got the same issue.
This question describes a similar problem, and tries to resolve it by merging the classes from both namespaces. This doesn't sound like a great solution to me, as I don't really need to customise the API authorize attribute.
What am I doing wrong here?
You can only share authorization with MVC basic authentification if MVC application is in the same project as Web API.
If not then you must create CustomAuthorize Attribute in Web API (inheriting http.AuthorizeAttribute) and implement basic authentification on WebAPI.
This way every request to WebAPI should pass userName and pass in AuthHeader of HTTP Request
I think the problem was that I was trying to call the API in server side code. (Don't ask, I know it doesn't sound logical).
Obviously, the server side request would not be impersonating the users credentials.
I tried setting <Identity Impersonate="true"/> in web.config but this had no effect.
To work abound this issue, I have moved the request to a JQuery ajax call on document ready which I should have just done in the first place.

Categories