6.4 KiB
title | author | description | monikerRange | ms.author | ms.date | uid |
---|---|---|---|---|---|---|
Add, download, and delete custom user data to Identity in an ASP.NET Core project | rick-anderson | Learn how to add custom user data to Identity in an ASP.NET Core project. Delete data per GDPR. | >= aspnetcore-2.1 | riande | 6/16/2018 | security/authentication/add-user-data |
Add, download, and delete custom user data to Identity in an ASP.NET Core project
This article shows how to:
- Add custom user data to an ASP.NET Core web app.
- Decorate the custom user data model with the PersonalData attribute so it's automatically available for download and deletion. Making the data able to be downloaded and deleted helps meet GDPR requirements.
The project sample is created from a Razor Pages web app, but the instructions are similar for a ASP.NET Core MVC web app.
View or download sample code (how to download)
Prerequisites
Create a Razor web app
Visual Studio
- From the Visual Studio File menu, select New > Project. Name the project WebApp1 if you want to it match the namespace of the download sample code.
- Select ASP.NET Core Web Application > OK
- Select ASP.NET Core 2.1 in the dropdown
- Select Web Application > OK
- Build and run the project.
.NET Core CLI
dotnet new webapp -o WebApp1
Run the Identity scaffolder
Visual Studio
- From Solution Explorer, right-click on the project > Add > New Scaffolded Item.
- From the left pane of the Add Scaffold dialog, select Identity > ADD.
- In the ADD Identity dialog, the following options:
- Select your existing layout file ~/Pages/Shared/_Layout.cshtml
- Select the following files to override:
- Account/Register
- Account/Manage/Index
- Select the + button to create a new Data context class. Accept the type (WebApp1.Models.WebApp1Context if you named the project WebApp1).
- Select the + button to create a new User class. Accept the type (WebApp1User if you named the project WebApp1) > Add.
- Select ADD.
.NET Core CLI
If you have not previously installed the ASP.NET scaffolder, install it now:
dotnet tool install -g dotnet-aspnet-codegenerator
Add a package reference to Microsoft.VisualStudio.Web.CodeGeneration.Design to the project (.csproj) file. Run the following command in the project directory:
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet restore
Run the following command to list the Identity scaffolder options:
dotnet aspnet-codegenerator identity -h
In the project folder, run the Identity scaffolder:
dotnet aspnet-codegenerator identity -u WebApp1User -fi Account.Register;Account.Manage.Index
Follow the instruction in Migrations, UseAuthentication, and layout to perform the following steps:
- Create a migration and update the database.
- Add
UseAuthentication
toStartup.Configure
. - Add
<partial name="_LoginPartial" />
to the layout file. - Test the app:
- Register a user
- Select the new user name (next to the Logout link). You might need to expand the window or select the navigation bar icon to show the user name and other links.
- Select the Personal Data tab.
- Select the Download button and examined the PersonalData.json file.
- Test the Delete button, which deletes the logged on user.
Add custom user data to the Identity DB
Update the IdentityUser
derived class with custom properties. If you named your project WebApp1, the file is named Areas/Identity/Data/WebApp1User.cs. Update the file with the following code:
[!code-csharpMain]
Properties decorated with the PersonalData attribute are:
- Deleted when the Areas/Identity/Pages/Account/Manage/DeletePersonalData.cshtml Razor Page calls
UserManager.Delete
. - Included in the downloaded data by the Areas/Identity/Pages/Account/Manage/DownloadPersonalData.cshtml Razor Page.
Update the Account/Manage/Index.cshtml page
Update the InputModel
in Areas/Identity/Pages/Account/Manage/Index.cshtml.cs with the following highlighted code:
[!code-csharpMain]
Update the Areas/Identity/Pages/Account/Manage/Index.cshtml with the following highlighted markup:
[!code-htmlMain]
Update the Account/Register.cshtml page
Update the InputModel
in Areas/Identity/Pages/Account/Register.cshtml.cs with the following highlighted code:
[!code-csharpMain]
Update the Areas/Identity/Pages/Account/Register.cshtml with the following highlighted markup:
[!code-htmlMain]
Build the project.
Add a migration for the custom user data
Visual Studio
In the Visual Studio Package Manager Console:
Add-Migration CustomUserData
Update-Database
.NET Core CLI
dotnet ef migrations add CustomUserData
dotnet ef database update
Test create, view, download, delete custom user data
Test the app:
- Register a new user.
- View the custom user data on the
/Identity/Account/Manage
page. - Download and view the users personal data from the
/Identity/Account/Manage/PersonalData
page.