How to enable CORS in .NET CORE Web API 6.0 | What is middleware in .NET CORE 6.0

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to nigeria techies this is our asp.net core tutorial 6.0 so part of this video i am going to explain two important topics the first one is cars and the second one is middleware so first let me explain what is cars so the car stands for cross section resource sharing this is the browser security feature that restricts the cross-section http request so then you have one rest ap you won't share the data to the different http request means you should enable the cars in our application first let me give one demo so after that i will explain how to enable these cars in our data core application so in my previous video i am just created one basic bb apa so we can see the outputs so this is the product service the first one is the get method i am trying to run it's from our swagger so it's returning true data okay so now i am trying to consume the service from one of the application so i am having an angular application so see i am trying to access the same url only once i got the data i am trying to bind in the one of the table totally we have the two fields one is id name price so i am trying to bind this data so we have two records so let me run the angular application also see so application in running mode let me go to the home so this is the table but there is no data binded so we can select this one we can see if any error so the console is clearly saying so access to the http request so this is actually our ap url and from the origin so this is our main application url that means our angular application url has been blocked by security policy so why this issue means be not enabled cars in our dot net core application i mean i mean in the vba ba i'm not enabled so now let me explain how to enable it let me go to the program.css so here first we need to define our course policy in the services once it's done we have to include in the upside so first let me register it so builder dot services dot add course so then we have to include the policy so add policy first we have to give our policy name course policy so after that we have to define what are the origins and what are the headers so we can define so let me show you so in the build we have option so if you want specific domain need to access your application you have to use this option with origins so next allow any method and allow any header so okay we have done so now in this place we have to provide the domain name so basically uh we can enable these cars the three types the first one is enabling the cars for single domain and the second one is for multiple domain and the final one is for any domain the service can be accessed in the any of the application there is no restriction based on the domain so we can start from the single domain so initially we are trying to consume the service in my angular application so the application url is localhost for w0 so let me copy this one and here i can include this one okay so initially i said once we registered our course policy we have to include in the app set then only it will work so let me come here add the app i mean use cars so here i have to promote the policy name so the policy name is cars policy so we have done our changes so let me run and we can verify okay our ap is running now so let me refresh the angular application so see now there is no error the records are loaded clearly now i am trying to change the domain i mean currently it's running in the 4 to w0 so instead of that i am trying to run in the different domain support i am trying to give the 3000 okay our application is running now so so let me change this port number here it's loaded now i'm coming to the home page so see now there is no data store loader so we can check in the console side so see again we are getting this car error because we enabled this cars for four to w0 only we not enabled for the three triple zero so let me include this domain also in the application i mean in the services so let me copy the same url so instead of this photo double zero i can use this three triple zero so now we have included two domains that's what the multiple domain so we know we want consume our baby in some dimensionally it could be three or four our maximum five domains means we can include like that if you are trying to access more than five domains we can use the file option any domain so we can check this one again i am running our ap application okay fine so let me refresh the screen so again we are in the port number three triple zero online but now see the datas are loaded fine so why because uh recently we have enabled these cars for this three triple zero also so now i am going to explain the final option any domain so for this one we have to remove all the existing domain so instead of that just to provide the star value that's enough so it will allow for any domains so we can verify now okay application is loaded so let me refresh the screen so see now again the datas are loaded so even if i'm trying to change the port number four to double zero or else if i'm trying to give some other port also it will work because we have enabled these cars for globally for any domains okay this is all about enabling the cars and hope you got the idea why we need to enable the cars what is cars so now let me move on this middleware first let me explain what is middleware in dartmouth core so the middleware are software components that are assembled to end an application pipeline to handle the request and response so just to consider any web obligation we are entering the application url in the browser so the request to go into the application it will process and it will provide the response the response is previewed in the browser that is the basic concept even the mvc application or baby apa the request is directly goes to the controller so based on the business logic it will process and it will return the response so before hitting to the controller if there is some configuration related things are there okay for doing that we have this middleware so in the darknet core the middlewares are defined in the program.cs file so then you have the question what are the middleware actually we are using so be how the n number of middlewares okay so let me start from the top so in the services first we have included this add db context okay so this is also one of the middleware so in case if you don't have this middleware the application will throw an error so in my product controller initially i am just injected this product api context okay the similar way i can inject for any controllers so if i don't have in this place it won't work okay the similar way we have enabled these cars and the same way we can register any interfaces also for registering the interfaces we have the add singleton option currently i don't have any interfaces so anyway let me show you how to add this one so builder dot so services dot add so single term so basically it contains two sections first section we have to provide the interface name so after that the implemented class name we have to provide here so once we added the same way we have to inject in our application i mean in the inject in the any of the controller so we can using it and a similar way we have added this add swagger generation so this is for enabling the swagger in our ap application the same way mostly in the any web application we will enable the authentication so it could be like a jwt or any authentication but we have to define the authentication policy in our application for that also we are using the same delver so builder dot services start add authentication so you know so here we have to provide the policies and other information based on that only our application will work so currently we don't have any authentication for our application so in my upcoming videos i am trying to explain how to add the authentication for generating the tokens we will see everything in the detail so now let me remove this one and also i want to convey one more information any request for our dot net core application the request is directly coming to this middleware only okay so if there is any error in the middleware it's automatically throw an error and also before getting to the controller it will check what are the dependencies are there like here i'm just injecting this db context the similar way if i'm trying to inject any interface also so it will check in the what is called the middleware if the middleware is not present it will automatically throw an error so anyway let me try to command it you can see so let me run the application so now application is loaded fine so when i'm trying to access this particular service see now i am getting this error because unable to resolve the service for type product apa context for fixing the issue be how to include in the middleware that's it the middleware point of view you already got the idea about this add db context and this odd cars and this swagger so i am not explained about this authentication we will cover in the upcoming videos and also the similar thing add singletons for including any interfaces so that also i'm going to cover in my next video thank you thanks for watching i hope you got some better idea about this cars and middleware still if you have any doubts or clarification please post in the comment box and also please don't forget to subscribe my channel thank you thanks for watching
Info
Channel: Nihira Techiees
Views: 32,792
Rating: undefined out of 5
Keywords: How to enable CORS in .NET CORE Web API 6.0, What is CORS, What is CORS in Web API, why we need to enable CORS, Advantage of CORS, What is cors policy, what is middleware, what is middleware in .net core, what is middleware in dot net core, how to resolve cors issue in Angular dot net core, what is Add DB Context, what is add singleton in dot net core, Dot Net Core Tutorial, cross orgin resource sharing, what are middleware in .net core
Id: XTQo2s3LDW0
Channel Id: undefined
Length: 14min 8sec (848 seconds)
Published: Tue Jun 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.