• About Me
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Advertise / Sponsor

DOTNET DETAIL

Learn Microsoft .NET Technologies

  • Home
  • Angular
    • Angular 5
    • Angular 6
  • Azure
  • ASP.NET Core
  • React
  • Vue
  • Cloud Hosting
  • Software
  • Interview Questions
    • Angular 6
    • ASP.NET Core
    • Microservices Interview Questions
You are here: Home / ASP.NET Core / User Secrets in Asp.Net Core 3.1

User Secrets in Asp.Net Core 3.1

May 22, 2020 by mebakar1005 Leave a Comment

In this tutorial, we are going to cover user secrets in Asp.Net Core 3.1 web application using visual studio code. First, we will understand what is secret manager in asp.net core and then we will implement all the user secrets options (E.g. set, remove, list, clear) in Asp.Net Core 3.1 web API application in step by step.

If you want to store some configuration information that can be accessed by the application during runtime, then the secret manager will help you to store such information. This information may include the web API keys, authenticated token keys, database connection strings, and etc. These are not user specific confidential values, but you still need to store in a very protected place.

Note: – In one of the previous tutorials, we have covered how to integrate react and asp.net core 3.1 application using vs code. So, we will not create a new web API project in this tutorial, we will use the same project which we have created in one of the previous tutorials.

What will you learn in this tutorial?

We will learn these below points in this tutorial.

  • How to enable user secrets in Asp.Net Core 3.1?
  • How to set user secrets in Asp.Net Core 3.1?
  • How to get the list of all the application secrets?
  • How to remove the specific secret from application?
  • How to access the user-secret values using configuration?

How to Implement User Secrets in Asp.Net Core 3.1 Application

Let’s start to implement “user secrets” in asp.net core 3.1 application using visual studio code.

READ  Project Folder Structure of ASP NET Core 2.0 Application

Step # 1: How to enable user secrets in Asp.Net Core?

So, go to the project terminal and then run this below command and it will give you some options that we can use in our application as you do see below in the screenshot.

dotnet user-secrets

It will give us some command options as you do see in the above highlighted area.

Note: – user-secrets are only available for development mode. If you are running production mode, then you will not have to access user-secrets.

Now, we need to add UserSecretsId in the API.csproj file. So, go to project folder structure and then go to the API.csproj file and then add the Id as you do see below in the file’s line # 14.

Note: – Here we are adding simply a guid generated id.

Step # 1: How to set user secrets in Asp.Net Core 3.1?

Now, in this step, we will see how to set user secrets in asp.net core 3.1 application. So, go to the API project terminal and then run the below command to set a user-secret as you do see below in the screenshot.

dotnet user-secrets set “TokenKey” “super secret key”

As you do see above, it has successfully saved the secret key into the secret store. 

Note: – the location of the secret store is the development machine. It is not located in the source code anywhere. It means, if you move your project to another system, then you will not have access to this token key and you will need to recreate the token key.

READ  How to integrate React and Asp.Net Core 3.1 app using VS Code

Step # 3: How to get the list of all the application secrets?

In this step, we will see how to get the list of all the application secrets. So, go to project terminal and then run the below command to get the list as you do see below in the screenshot.

=> dotnet user-secrets list

As you do see above, all the secrets are available. 

Step # 4: How to remove the specific user secret?

Now, in this step, we will see how to remove the specific secret from the application secrets. So, run this below command as you do see below in the screenshot.

dotnet user-secrets remove “TokenKey”

As you do see above, the first command removes the TokenKey secret and the second command is getting the list of secrets. But as you see there is no secret configured for this application.

Step # 5: How to clear the user secrets?

Now, in this step, we will see how to clear all the user secrets from the application. So, run the below command and it will clear all the secrets as you do see below in the screenshot.

Note: – If you want to remove or set or clear the secrets from the specific project or a folder location that contains the .csproj file, then you can use the –project or –p flag and project or folder location as you do see below in the screenshot. 

Step # 6: How to access the user-secret values using configuration?

Now, in this step, we will see how to access the user-secret values using the configuration. As you know we are working on a project which we have created in one of the previous tutorials. So,  in one of the previous tutorials, we have discussed JWT authentication in Asp.Net Core 3.1 using vs code. So, in that project we have covered how to create a token as do see below in the file. 

In the above file’s line # 20, we are assigning the token key “super secret key”. But now, we will use the TokenKey from the user secrets using the configuration, which we have created above. Let’s see how to do it in the below file.

Let’s understand the above code changes.

READ  How to add Authentication to React app using Asp.Net Core 3 in VS2019

Line # 17 to 20: here in this block of code, we are injecting the IConfiguration using constructor.

Line # 28: here in this line, we are accessing the token key using configuration.

Now, in the same way we need to change it in the startup class. So, go to the project startup class and then add some changes as you do see below in the file’s line # 64.

Note:- If you found this blog helpful then: Buy me a coffeeBuy me a coffee
JWT Authentication in Asp.Net Core 3.1
Key Vault secrets in Asp.Net Core 3.1

Related

Filed Under: ASP.NET Core, web API Tagged With: App Secrets, Asp.Net Core 3.1, User Secrets

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Buy me a coffeeBuy me a coffee

Best Asp.Net Hosting

Join Us

Join Us

Subscribe to Blog via Email

Enter your email address to subscribe.

One of the best material to learn Angular

Recent Posts

  • Best Cheap cloud hosting in 2021
  • Top Microservices Interview Questions and Answers
  • Best Cloud Hosting Services in 2021
  • Implement API Versioning in Asp Net Core 5 Web API
  • 10 Best 3D Printing Software of 2021
  • Build a Microservice using Asp Net Core 5 and Docker

Tags

.NET CORE .NET Core 2.0 .NET Core 3.0 .NET Core 3.1 .NET Core 5 2FA ANGULAR ANGULAR 5 ANGULAR 5 TUTORIAL Angular 6 Angular 6 Tutorial Angular 7 Angular 7 Tutorials ANGULAR AND .NET CORE ANGULAR TUTORIAL ASP.NET ASP.NET CORE ASP.NET CORE 2.0 ASP.NET Core 2.1 Asp.Net Core 3.0 Asp.Net Core 3.1 Asp.Net Core 5 ASP.NET Core Authentication ASP.NET Core MVC ASP.NET Core Routing ASP.NET CORE TUTORIAL ASP.NET MVC6 Authentication Authentication and Authorization Azure CRUD Deploy Asp.Net Core App to azure Deployment EF Core ENTITY FRAMEWORK Entity Framework Core Interview Questions JWT Microservices MVC 6 Reactjs Two Factor Authentication User Secrets Visual Studio 2019 WEB API
Copyright © 2021