ASP.NET Core Web API Publish To IIS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back in this lesson we will start setting up our environment for a local deployment of our api known as when we say local deployment this could easily be your personal machine so in my situation i'll be deploying it on my personal machine but it could also be on a company machine or a server somewhere that this api needs to serve you know across the network and it could potentially even be to the internet whatever the situation i'll be teaching you or showing you critical steps towards prepping this machine so that it can host your api so our journey starts off here where we have to download the.net 5 runtime we already have the sdk which we've been using for development but we want to make sure we have the runtime and we can just get that hosting bundle for windows so when you do that you launch your installer and then you go ahead and install it go through any security checkpoints and once that is done you can close that and then now we're going to move on to setting up iis or internet information services so what we want to do is add so you can just go to your start menu and say add feature and then you want to click turn windows features on or off so once that is selected this little box will pop up and you want to make sure that you select internet information services once you do that you're going to get that square that square means that you know it's not everything that is ticked which is fine you don't need every single feature but i'm going to show you the ones that i have currently installed and then you can just go through and make sure that those are also ticked on your side so i have the management console and then i have these libraries for application development features so you can make sure that you take these off maybe you don't need all of them but it works for me with these so i would recommend that you make sure you have those and then for http features you can go ahead and tick those health and diagnostics once well that section is actually optional but i recommend that you get the http logging section and then performance features basic authentication and the others so once you have those you can click ok and then it may take a while to install i already have them so i don't need to wait for them to install but once they finish installing then you can resume this video so once you're sure that you have is or that process is finished to make sure you have iis you can just go to your start menu and type in iis and then you see it pop up and then when you launch you get this console showing you your server and all of the sites that you have currently listed under there all right to make sure that it's working you can actually just click on default website and go to browse and then it will show you this page which shows you that you're now seeing your server now another essential part of setting up your local environment will be to make sure that your database server is up installed and running so when we talk about the database server we have a few options in an enterprise setting you probably already have it or you should have it you can check with your system administrator whatever you need to do but you just go ahead and make sure you have sql server on a personal level if you don't already have it you have two options you have either developer or express so express is very free um no licensing required at all and but then it has some limitations on a personal level express is fine um developer gives you far more features and it's actually full powered so it's a much bigger download and a much bigger task to have it installed it gives you access to many more features for this deployment you probably can get away with express but then maybe if you're thinking about longevity you probably just want to get developers so you have everything you need so you can go ahead and download that the installation is pretty straightforward and at the end of it you'll be prompted to install the management studio and just in case you missed that step you can always just google ssms which is short for sql server management studio and once you go through that you can always just download the management studio through this medium also which is also a straightforward installation so to launch the management studio after it's installed and to connect to your database server you can go ahead and click the icon and you'll see this splash launching screen and to connect to the server you'll have to put in the server name so i have two servers running on my well a few servers running on my machine right now i have sql express so i do have an instance of sql express but i also have an instance of the developer so to get to sql express you can say dot backslash sql express that dot really means localhost also right and then if you went with developer then you just need localhost so you wouldn't need any slash anything else so i'm going to use developer so if i say localhost and it will connect and i think i have a few databases not that too many so we can proceed now before we move to the next step i just want to encourage you make sure that you have admin access or even if you don't have administrative access if you're in an enterprise setting where you're not in a position to be the administrator that you at least have enough access or you are working with somebody who has the access to carry out some of the actions here so a lot of the problems that people encounter when doing these kind of deployments outside of probably missing out a library not installing something the next biggest issue is usually permissions and author authorizations right so just make sure that you're working with somebody if if that is the case who has enough permissions to go around any potential roadblocks and if you're on your personal machine just or a machine that is yours to manage just make sure that you have administrative rights hey guys welcome back in this lesson we'll start setting up our environment for a local deployment of our api known as when we say local deployment this could easily be your personal machine so in my situation i'll be deploying it on my personal machine but it could also be on a company machine or a server somewhere that this api needs to serve you know across the network and it could potentially even be to the internet whatever the situation i'll be teaching you or showing you critical steps towards prepping this machine so that it can host your api so our journey starts off here where we have to download the dot net 5 runtime we already have the sdk which we've been using for development but we want to make sure we have the runtime and we can just get that hosting bundle for windows so when you do that you launch your installer and then you go ahead and install it go through any security checkpoints and once that is done you can close that and then now we are going to move on to setting up iis or internet information services so what we want to do is add so you can just go to your start menu and say add feature and then you want to click turn windows features on or off so once that is selected this little box will pop up and you want to make sure that you select internet information services once you do that you're going to get that square that square means that you know it's not everything that is ticked which is fine you don't need every single feature but i'm going to show you the ones that i have currently installed and then you can just go through and make sure that those are also ticked on your side so i have the management console and then i have these libraries for application development feature so you can make sure that you take these off maybe you don't need all of them but it works for me with these so i would recommend that you make sure you have those and then for http features you can go ahead and tick those health and diagnostics once it well that section is actually optional but i recommend that you get the http logging section and then performance features basic authentication and the others so once you have those you can click ok and then it may take a while to install i already have them so i don't need to wait for them to install but once they finish installing then you can resume this video so once you're sure that you have is or that process is finished to make sure you have iis you can just go to your start menu and type in iis and then you see it pop up and then when you launch you get this console showing you your server and all of the sites that you have currently listed under there all right to make sure that it's working you can actually just click on default website and go to browse and then it will show you this page which shows you that you're now seeing your server now another essential part of setting up your local environment will be to make sure that your database server is up installed and running so when we talk about the database server we have a few options in an enterprise setting you probably already have it or you should have it you can check with your system administrator whatever you need to do but you just go ahead and make sure you have sql server on a personal level if you don't already have it you have two options you have either developer or express so express is very free um no licensing required at all and but then it has some limitations so on a personal level express is fine um developer gives you far more features and it's actually full powered so it's a much bigger download and a much bigger task to have it installed it gives you access to many more features for this deployment you probably can get away with express but then maybe if you're thinking about longevity you probably just want to get developers so you have everything you need so you can go ahead and download that the installation is pretty straightforward and at the end of it you'll be prompted to install the management studio and just in case you missed that step you can always just google ssms which is short for sql server management studio and once you go through that you can always just download the management studio through this medium also which is also a straightforward installation so to launch the management studio after it's installed and to connect to your database server you can go ahead and click the icon and you'll see this splash launching screen and to connect to the server you'll have to put in the server name so i have two servers running on my or a few servers running on my machine right now i have a sql express so i do have an instance of sql express but i also have an instance of the developer so to get to sql express you can say dot backslash sql express that that really means localhost also right and then if you went with developer then you just need localhost so you wouldn't need any slash anything else so i'm going to use developer so if i say localhost and it will connect and i think i have a few databases not that too many so we can proceed now before we move to the next step i just want to encourage you make sure that you have admin access or even if you don't have administrative access if you're in an enterprise setting where you're not in a position to be the administrator that you at least have enough access or you are working with somebody who has the access to carry out some of the actions here so a lot of the problems that people encounter when doing these kind of deployments outside of probably missing out a library not installing something the next biggest issue is usually permissions and author authorizations right so just make sure that you're working with somebody if if that is the case who has enough permissions to go around any potential roadblocks and if you're on your personal machine just or a machine that is yours to manage just make sure that you have administrative rights all right guys so after setting up your environment and i'm sure you've vetted that everything works now what we want to do is actually publish the site to our iis now before we do that we have a few steps that we have to complete and one code change in particular that i want to do is regarding the swagger ui so in the startup page where we have app.use swagger ui i want to kind of change how this endpoint is generated and the reason is that i have witnessed that once it is published the path rules kind of change so what we want to do is make it look something more like this where use server ui or c lambda token but then we have the curly braces making it an object and we're just going to kind of qualify the base path a bit to say string dot is no lower white space c dot root prefix all right and then we're going to say dot or dot dot so what happens is that when the root prefix changes relative to if it's in development like it is now or if it's published the the path needs to look a little bit different so what we're doing is just saying either put one dot or two dots and then we're just going to prepend that to our existing path which was there before and hotel listing api all right so you can go ahead and make that quick change and just to double check that it still works we can run and we're still seeing our swagger you api documentation and you'd also notice that the url has that dot dot all right when we click it we get this so when we go to iis that that link might look slightly differently which is why we went through all of what we just did all right so now that we confirm that is working another very important thing that we want to do is create the database so in our database server so whether you're using sql express or you're using developer you can just go to that particular instance that you hope to you know host the api's web database sorry on and then you just want to create a new database so you want the database to be present for when we do our migrations right so because i don't want any typos i'm just going to double click the database name from the from the hosts from the app settings sorry and then just go ahead and use that name to create the database and then you can just go ahead and confirm that the database is there it's empty right now don't worry too much about it but this is the initial deployment right so what we want to do is publish so we can go ahead right click our project we see publish click that and then we can choose our target so we have quite a few options here you can publish to azure which we can look at later on to docker a folder ftp this would come in handy if you're hosting like on a you know godaddy or one of those or you have to ftp to the server that it will be hosted on or you can go directly to iis or if you have other profiles you can just import a profile so i'm going to do folder and i'm going to explain why i do folder because i'm doing a local deployment i can do it locally to the machine and then move the files to where i want them to go this also applies when you're going and this is my based on my experience so in my experience i have done folder deployments where i chose the folder directly on the server it was going to because of a typo i put in the wrong path and you know i tried it and tried it and tried it and nothing was updating it turns out i was doing it to the wrong path i didn't notice until i chose an option that said delete everything in the file folder and then i ended up removing some other critical files just to get my little website in so my cautious nature now that i've learned through experience prompts me to always do a deployment to my machine and then take the files and move them to the target machine that's how i do it so that's what i'm going to demonstrate to you based on the experience you can choose whether or not you will do it my way or you just go straight to the target machine but there is no right or wrong way it's just a matter of um experience teaching me wisdom all right so i'm going to choose a local folder deployment so it's going to go to the bin folder release.net 5 publish all right we can click finish and then we can edit so there's certain things that we want to do so we can at this point change the target location if you wish but then when we go to settings we can choose other little configurations i want the file publish options so this is the option i was talking about delete all existing files per so what it does is wipe the folder and then just put the new files and if you don't choose that what it will do is probably just override what's there or what's in common anyway so this can be risky because if it's a wrong folder like i said it will remove critical files we can choose the databases now databases use this connection string at runtime and i'm going to tick this and i'm going to change this connection stream because i don't want it to use local dbms sql localdb i wanted to use my local host or localhost sql express whatever the server name is you put that there i will retain the same database name and any other option and then they ask about the migration so i'm going to say apply this migration and what this will do is generate a script based on the current situation of the database and the migrations and everything right so we can just go ahead and do that click save and then click publish so here you have the target location this is where it's going to go on my machine publish and once that publish is completed they'll show you if it was successful or not which it was in my case and the generation of the scripts was completed successfully so moving on from it just being the initial deployment this script generation step is very important because when you're going to be you know deploying like an update it's already in production and you're updating something i made database changes new migrations etc you will want to provide that script to the database administrator or the senior developer i mean if you're the senior developer then fine but you'll want this script in order to more have more control over the database changes you don't want anybody to just say oh i'm migrating data and i'm just going to do a migration on the database because in development we have fewer concerns about the actual data and integrity than in production so you want to be careful of that anyhow let's move forward so i can just control click and go directly to the file system where this was published and i can take those files and what i'm going to do is in iis so let me just bring up is we already have the default website we know this we're going to right click and then i'm just going to go to explore so this is now bringing me to the init pub folder where and www root folder where all web apps must live now let's just say that this is a server that has multiple word applications running so all of them are running from the server name slash the web app name so what we're going to do is create the api as a web app so we'll get the server name or the machine name slash the apis endpoints all right so i'm going to create a folder i'm just going to call it hotel underscore api and then inside this folder i'm going to place all the files so if you're copying across a network i would you know recommend that you archive them so yeah you know it's easier to copy across but since it's just from photo folder on the same machine here it was an easy operation then i want to run the sql script so i'm just going to go into ef sorry canada that quickly efs qrs scripts open that script file and then here you'll see that everything that was in the migrations is right here in nice understandable sql all right so even the migrations that we did database created all of those things seeding data so the default data that we put in all of those migrations are here in sql scripts so that's what i'm saying that when we're going to be changing the database each time the script is generated we can actually extract the new stuff and have more control over what we're doing to the database all right so here and well either way the script kind of protects you because they say if the so that's why i have the migration history table because it takes the snapshots of each migration so it says if this migration doesn't exist then carry out this action so it kind of will say if it's done already i don't need to do it again all right so we can just execute this and let me just go back to the top so with sql if you're if you're you know not so familiar with sql in order to make sure that your script is able to run against a cart database you can either at the top say use and you put in the database names in this case it will be hotel listing or you just make sure that the database is selected from the top here in the list of available databases right so that's so you can ensure that you're able to run it on the current database when i click execute the script runs successfully so now i have the tables if i just refresh they're all there all the ones for the users and the hotels and countries all the ones we created manually they're all there so now that the database is primed and ready what i'm going to do is continue with setting up the api web app itself so back in iis i'm just going to refresh this list because what it will do is list every folder that is in www root automatically see that and then what i'm going to do is right click it and convert to an application uh before i do that i'm sorry i'm sorry before we convert this to an application let's go to the application pool so an application pool is like a container within which a web application or web applications can run over a single responsibility principle would suggest that you do a pool per app right because if apple goes on with multiple apps then all those apps stop working so you know for isolation you can have multiple posts i'm going to name it hotel api pool not to nothing too fancy or complicated the net clr version i'm going to say no manage code because we're using.net core so this is very very important and then we can click ok so that's fine now the relevance of that is that when i'm coming back to the previous step now when i right click and say convert to application it's going to ask me which application pool i want to select so i can go here and select from this drop down list hotel api pool that's the one that i wish for this app to run in and then i can go ahead and click ok and then you'll notice that it no longer looks like a folder no it looks like a little web globe with the well no it looks like an app all right so let's take this for a spin i'm going to just browse to port and we land on this page that is saying 404 address not found all right but what we would have expected is more like the swagger documentation so let me see if maybe when i prompt it it will and there we go so now at least we know that we're looking at it from localhost so notice this is not localhost colon the ports localhost call on the ports the port number let me just show you just so we're very clear localhost colon the port number represents the dev environment in my situation local host by itself represents iis if you have multiple web web servers maybe you have one for example well apache or other web servers running then you may need to change the ports because right now i have apache but it runs on localhost port 8080 right but 80 is the default and is is my default so localhost is what you see where the web app name and then the first page so at least we know that we're serving up activity from the iis all right so let us use postman and do a fresh request so i'm just going to request all the hotels from the api now notice the link i hope it's not too small right now i have to say localhost slash hotel underscore api slash api slash hotel all right so it's local slash the web app name slash uh the path that we would have set up in our controller so just to jump back over and i'm just doing this so we have a complete understanding of all these little variables and how they play a part right so the route that we set was api slash controller i did say we could change it and we probably will know that we're deploying right but the point is that because it's api that means whatever is preceding it will always go that way so if it is that we didn't use our web app and call it total up it could have been localhost slash api slash hotel if it was the main route if it was a route location of the application server so in this case it is not i'm not going to go into any renaming just because because your situation might be different from mine i'm just explaining all the dynamics behind why my link may look this way and yours may not because you went on a different path so if you deploy directly and once again i have no problem showing if you deployed your your api directly to the root of this inet pub right of www root sorry you didn't make a subfolder but you put it right here at the root then localhost swagger would actually serve you up this very page that we were just looking at in coming from localhost right but because of this web app we have to go into the web app to get to the actual stuff all right so let us test this request and when i click send i am getting a 500 error now the cool thing about all of what we did was that we set up logging right so i can always just go over to my logs folder and take a look at why i'm getting these errors all right so this log file is showing me that something went wrong in the data client it there was a login failure for the user and then it's showing me the name of the user so in the previous video i did say permissions will play a very important part in what you're doing right so for our app pool that we just created in in order for the user underneath the up pool to be able to interact with the database we need to make sure that we give it permission to the database so what i'm going to do is go over to sql server go back to the database and then we're going to do two things we're going to create a login for that app pooled user and then we're going to add that login to the security for the database so firstly new login or well let me let me just see if i can do it directly as a user so under the database itself i'll go to users security then users then new user and then i'll choose windows user all right and then i'll to put in that username is up pool that's its domain and the basically the name of the pool is the username all right and we can click ok and if we look in the list then we'll see that that app pool user is now added now let me try this request again still getting a 500 let me tweet this a bit so what i will need to do is give it more permissions all right so we added it to the database but we didn't give it any membership so i'm just going to sit as db owner now you you will want to probably be a bit more granular than i am being right now uh when setting permissions because the user that may access the database might not you might not want to give them ownership because ownership means they can drop the database and do other potentially malicious things as much as it's a system user you want to be very security conscious when giving permissions right so you may just want to give them data reader data rather which means they can read and write to the database but they can't do any other critical operation that might compromise the data all right in my situation it's my machine they're my users i'm going to set bb owner um so i'm going to say membership db owner and own schemas db owner and if you look back at general you notice that the default schema is dbo all right that you don't have to worry about that that's more database level stuff we just want to get our application up and running so we can click ok and then we can try again and then we see here that we're getting back data so we're getting data from our api database now just to let you know that some of the security features or let me not say features the configurations that i might have just put in might not necessarily work just like that for you there are other factors are you the administrator doing these things um do you have enough permission to give users permissions and all of those things do play a role in whether or not you can successfully complete all the activities like you just saw me do it ultimately however these are the very basic steps towards getting your application up and running um and the configurations needed on the database and i sorry yes now going back to iis there are few things that we want to make sure we get in place if you remember when we were setting up our jwt we had added a secret key that we added as an environment variable now if you added it to the app settings file then you don't have to go through this step if however you did what i did and added it as a system variable then we need our api to store that key right so that it can handle that operation by itself wherever it is on whatever machine so we're going to in is go over to the configuration editor and from the drop down list to the left we want to make sure that we have system dot web server and then we're going to choose asp.net core all right so go ahead click section and we're going to system.web server asp.net core and then from the dropdown to the right we're going down to applicationhost.com config now when we do those two changes we can see environment variables count equals zero so we're just going to click that and then we're going to add our environment variable now the same thing that you had added as the environment key so i just went and retrieved mine and my name was the word key and my value was this grid now however complicated however simply it was the point is that we want it in a secure location and that is what this mechanism is affording us all right so you can just go ahead and put in your name and the key that you selected and then we can go ahead and close this now that it's added and it's the count is now one we can click apply to apply the changes and then restart our pool so remember i did mention that when multiple apps are in the pool if you if anything happens to the pool then all of those apps go down so we just want to restart our pool we can just do a recycle and then what we want to do now is test our login and token retrieval and then secured access so let me just double check that this still work all right so our api still works that's good let me attempt to log in so i'm just going to go back to one of my older older requests right and well actually i can't use this older request yet because if i attempt it so i'm just going to duplicate this and i'm going to remove the port number and i'm also not using https https that's a whole other level of configuration we're not going to get too much into that in this in this lesson we just want to get our deployment up and running so localhost slash hotel underscore api then slash api slash logins so when i send i'm getting a 401 right i'm not authorized because this user does not exist so because we didn't see the user so if you if you see that the user you probably don't have that problem so i'm just going to close some of these older requests all right so let's try and register the same user all right same user so just change the path and when we send the request we see that we get a tool to accept it so this user is created let's go to the database and verb and we can see that user here trevor example.com and the password all right so now that we have the user created let us try our login operation again to try and get the token so the same request that we just tried let me send and here is our token all right so if we just go to jwt and take a look at our token we're seeing things that we're used to seeing right um it's going to say invalid signature because once again it doesn't know the key it was signed with which is what we just set up in the environment variable but we see that the token does have the information that we expect so that's now i'm going to try one of the delete operations so let me try it with the hotel so we know that we have three hotels that we seeded right i'm going to delete hotel with id3 from the database so we know that we need our token and we have the link let me just change this to hotel so we're deleting hotels so we just want to verify that our token works and when we do that we're getting back at 204 so that means if i go to the database and look in the hotels table that started off with three we're only seeing two so you see our token worked um are all our endpoints at least what we've tested so far work and so far we have successfully deployed our api to our local you know local is deployment with our local database supports so following these guidelines you could successfully deploy your api within your company setting or for your personal use or you know whatever the situation might be [Music]
Info
Channel: Trevoir Williams
Views: 15,735
Rating: undefined out of 5
Keywords: asp core visual studio 2022, asp core web api, asp.net core publish to iis, asp.net core tutorial, asp.net core web api publish to iis, authentication, authorization, how to publish asp.net core aplication on iis, how to publish asp.net core application on iis, how to publish asp.net web application in iis, how to publish asp.net website on iis, iis manager, server manager, visual studio 2022 tutorial, visual studio 2022, asp.net core, software development
Id: PdsgiiLzQ18
Channel Id: undefined
Length: 38min 28sec (2308 seconds)
Published: Mon Mar 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.