✔ Manage User Secrets in Asp.Net Core. Change Connectionstring to secrets.json Environment variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video i will show you how to remove the secrets some secret strings using password combinations from your asp.net core application so when you scaffold your data model so then your then you get this context file and there is the sql server string and we don't want those strings in the application where there are secrets so how will we get out of this one position is that when you have an application uh you have here those connection connected services this is one solution to guess this one now when you click on this you will see there's a secrets.json local uh sql server express so this means when we have the uh the machine on our own production machine nearby so we need uh only to connect it um so this is sql server as a local db it's on the same place where my machine is and we don't need a user password combination for this but but it's possible and the other way is we have here those uh when you click on project you go here to manage user secrets so this file is outside the project it's not here in our project so even if you go there to our open full path folder there there's nowhere this file so the this the string will be placed here on the secret search json when we hover over it you see that's see backslash users and then there's the username and then there comes a lot of stuff and can use the secrets and then usersecrets.json so this file is outside and it loads those files there's from this block from this web host application from the environment so the environment here is under system somewhere so this is outside and it first tries to get the values from this file so we are able to write our stuff outside all the combinations passwords and all the stuff secrets and and we will place it now in our application so we do it if we have the sql server express we do it like this we go here in the under our edit dependency and then we write something like this connection string database see in a labeler title where a key how we can find it and then there's the connection string server and then there's a lot a bigger big stuff there and that's the default which is in there so uh we are able now to okay to take our stuff from here there from here and we cut it out and we drop it in the other machine so this should work but there will be an error after this and because it only accepts these local stuff so it says local local db means our local machine you may also write here a dot but it's overriding that point and then we say sql server sql express that's my machine the application which is running so we have this in our the string and we had something with the database the database was called here connected true let's drop this out the database is called here api rig record time records yes and that's it and now it's loading save saves those values in the secrets.json local so when i say next it drops that stuff in it does something with a nougat package i don't know what this is why it does any stuff with this in the secret store so i place it there and now we have it automatically in our json string either we go here by this uh we have a look in there so this is the same what we see here when we go there and say manage manage look at package you see it's the same ah excuse me it was too fast this one managed to manage user secrets now this is the same string that's uh just by another way and then we have the connection strings double point and then the key and then there is the string which which we have placed in there and there is uh here in here we have uh since it's we need in this stuff in c sharp with a uh with two lines ah you see that's that's why it's uh writing this one was wrong and that put this position when we have written it so you have to you see we have to replace something in here and we need in the string only uh if we have only one backslash then we need we need two in the environment we need only one backslash and there is now our string and when we place it we might close it see and now this is done it's now in that file and we have it now in our in our uh we may delete it now in our um we have already deleted it here so now we need the string in here from the connection file see in something yeah you know our secret uh the key but how does it go there and for this we need the configuration and the configuration either we make here a using file in there or we take it we go this way we delete this one from the connection file and as you see this this is protected override void on configuring this is when we start when we inject the db context and there's the options builder options builder by db context options builder now i deleted that block closing that file now we do the following we always have to uh when the startup file comes we have to connect the database or the database which is somewhere somewhere and this is usually done by when we say okay something like this services at db context and then the model class which is in there so this is why the contact contact is that's the our project under the folder models in this case and here is our our class that's what it does and by default uh you don't have to do anything if we leave the the stuff which was there and we will do it in this way since we used are now our secrets go back we don't want it this way we take it this way we say services at db context and now again our model our class then we say options builder which is the same which we had inside there we say option options builder goes to options builder and then we have those the the same lines inside that's a writing of this inline method option goes to and now we have the inline method uh options builder use seek use here this you see now the intellisense is there and you see that it's using the sql server and for this you need to implement uh here in the top is the implementing of the configuration that's the configuration comes in here when the system starts it drops here the in the startup it injects the configuration and from this it takes the values out from outside from this secrets json and it binds this and we need also the entity framework the entity framework is in you don't you need it in this case for where's my line uh yeah that is for use sql server so this is under the uh entity framework then we say configuration get connection string and now we use the configuration we go to the json file say get connection string and then we take the cn database and that's it so there's no code in our file anymore now when i start my my application where is it here's my application and it's an asp.net core api version and now when i try here to run that stuff you will see there is an error something is wrong with it but uh it should work you know now i have to look up what's what's going on there's something wrong with it maybe it's from the uh sql server is sql express database uh there's something wrong in our string well i will clear that but uh as you know this works it says okay it gets the value for at this point now i make it say a breakpoint and start this again and now the debugger will come does it doesn't it come in there restart i'm not sure if i will get it at the point here and now we are able to get the value at runtime and in there we will see under the configuration i i think i should see it somewhere there's the secrets json file and there are the data and inside the data we have this connection string so you see uh it's under in the runtime it binds the values from this from this file and i've written those uh that stuff ah now one more thing when you're now i'm in development system and developer development system everything is okay when you go to the uh production system then you have to add this one in the environment and the either if you have a window system that's okay then you may do it manually otherwise you do it like this when you say the dot net commands when you use the dot net commands and i've written it somewhere you will find it in the code uh there you say open the uh powershell or something like this and then you say dotnet user secrets since you always need net in this case uh user secrets set connection string and then you write the same stuff in there and if you are not in the folder then you say project folder and then that's the folder what you need so this is the same what we have and this is there's the point with only one backslash in the environment so even in the environment you keep all those variables outside of your project files and if you like this then make a thumb up
Info
Channel: CodeDocu Developer C# Asp Net Angular
Views: 10,774
Rating: undefined out of 5
Keywords: Windows;Android
Id: 7OBMhoKieqk
Channel Id: undefined
Length: 12min 29sec (749 seconds)
Published: Thu Jan 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.