How to Enable CORS and Fix Access-Control-Allow-Origin in ASP.NET Core 6 API?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome to see Subspace today in this video session we will discuss about how to fix the course and allow access origin problem in dotnet 6 web API so let's start I open my widget studio and here I already create a one project for API this is the sample project or simply a login by matching their username and password on the hop up this details so I will run this application so uh here is my idea is to access this uh login API with another project or one any other static pages so I I also create one sample for static page this is the simple HTML page that accessing that URL that that here we create a uh API for login it's like a 70 pipette and here I will access that so if we run this application so if we access that uh login API then this will send a message so a message for course errors so we can check this inside this console Tech population has been blocked by cross policy so for resolving this errors we need to add some more changes in our web API project so first of all uh we need to here uh we need to create a one more variable like I am getting were where where I am adding any variables like my allow a specific Origins is equal to and here here we provide any name like I'm providing with my allow specific Origins and then here we add a here we add a code for building these services so it's like a Builder dot Services dot add course lead course then uh here we provide like options options options and then its options dot add policy and then here we provide a name it's a name like my name is a variable name is my alloy specific Origins and then we provide the policy policy Lambda Express and then here it's a policy dot with with Origins we set here our origin name our origin name is uh this this project this this is a we copy this name here you can set your actual website names or is the main domain so and currently this is localhost so I'm copy this and paste it this to here and then uh we here we allow the any headers so you can also specify any specific headers so for example here I am adding this any headers and then allow any method new method's name now we add this course to here before the run application so it's app uh Dot use course ended it's like here we provide our specific course variable so it's my yellow specific results then we save this application so now we are in this application so I am pressing Ctrl F5 to run this application so now this website is listening on this port and now I am running this static pages so I'm pressing Ctrl F5 is also and then we call that a static page I am writing here any username and password think again this showing a course message I think something went wrong with our code so I am going to here and we will remove this slash because this is not record we only require here or domain name so I remove this and again and build this application and now I again go to this and I am writing any name your username and password so click on login now this showing a failed message so I already saw add this message inside this our project web API projects so displayed and success so if I adding in so we entered this correct details like username and password so I am enter it here it's admin and admin123 I'm pressing login this then this will show a success matches so this will already add to here the when username and password is match so this is the complete code for resolving the course and access control issues so this changes is required uh in your existing apis like we creating one variables then we add a services for that course and then we add that course before our run method so this is the complete code and if you have any questions any problem please write in comment box I will reply soon and if you like my videos please click on subscribe button and for latest update also click on notification Bell so thanks thanks for watching we will meet in next video session
Info
Channel: Csharp Space
Views: 8,297
Rating: undefined out of 5
Keywords: XMLHttpRequest, CORS policy, Access-Control-Allow-Origin, Access to XMLHttpRequest at from origin has been blocked by CORS policy, csharp space, cors error in .net 6 api, Response to preflight request doesn't pass access control check, cors origin error, cross origin resource sharing, cors error, cors explained
Id: TtETt3kjQwo
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Sun Jan 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.