AspNetCore.Docs/aspnetcore/security/authentication/identity-primary-key-config...

48 lines
2.9 KiB
Markdown
Raw Normal View History

---
title: Configure Identity primary key data type in ASP.NET Core
2017-09-28 23:05:12 +08:00
author: AdrienTorris
description: Learn about the steps for configuring the desired data type used for the ASP.NET Core Identity primary key.
2018-01-29 23:21:31 +08:00
ms.author: scaddie
2017-09-28 23:05:12 +08:00
ms.date: 09/28/2017
uid: security/authentication/identity-primary-key-configuration
---
# Configure Identity primary key data type in ASP.NET Core
2017-09-29 04:13:03 +08:00
ASP.NET Core Identity allows you to configure the data type used to represent a primary key. Identity uses the `string` data type by default. You can override this behavior.
2017-09-29 04:13:03 +08:00
## Customize the primary key data type
1. Create a custom implementation of the [IdentityUser](/dotnet/api/microsoft.aspnetcore.identity.entityframeworkcore.identityuser-1) class. It represents the type to be used for creating user objects. In the following example, the default `string` type is replaced with `Guid`.
[!code-csharp[](identity/sample/src/ASPNET-IdentityDemo-PrimaryKeysConfig/Models/ApplicationUser.cs?highlight=4&range=7-13)]
2. Create a custom implementation of the [IdentityRole](/dotnet/api/microsoft.aspnetcore.identity.entityframeworkcore.identityrole-1) class. It represents the type to be used for creating role objects. In the following example, the default `string` type is replaced with `Guid`.
2018-04-05 07:51:35 +08:00
[!code-csharp[](identity/sample/src/ASPNET-IdentityDemo-PrimaryKeysConfig/Models/ApplicationRole.cs?highlight=3&range=7-12)]
2018-04-05 07:51:35 +08:00
3. Create a custom database context class. It inherits from the Entity Framework database context class used for Identity. The `TUser` and `TRole` arguments reference the custom user and role classes created in the previous step, respectively. The `Guid` data type is defined for the primary key.
[!code-csharp[](identity/sample/src/ASPNET-IdentityDemo-PrimaryKeysConfig/Data/ApplicationDbContext.cs?highlight=3&range=9-26)]
2018-04-05 07:51:35 +08:00
4. Register the custom database context class when adding the Identity service in the app's startup class.
2018-05-11 08:58:40 +08:00
# [ASP.NET Core 2.x](#tab/aspnetcore2x/)
2018-04-05 07:51:35 +08:00
2018-05-11 08:58:40 +08:00
The `AddEntityFrameworkStores` method doesn't accept a `TKey` argument as it did in ASP.NET Core 1.x. The primary key's data type is inferred by analyzing the `DbContext` object.
2018-04-05 07:51:35 +08:00
2018-05-11 08:58:40 +08:00
[!code-csharp[](identity/sample/src/ASPNETv2-IdentityDemo-PrimaryKeysConfig/Startup.cs?highlight=6-8&range=25-37)]
2018-04-05 07:51:35 +08:00
2018-05-11 08:58:40 +08:00
# [ASP.NET Core 1.x](#tab/aspnetcore1x/)
The `AddEntityFrameworkStores` method accepts a `TKey` argument indicating the primary key's data type.
[!code-csharp[](identity/sample/src/ASPNET-IdentityDemo-PrimaryKeysConfig/Startup.cs?highlight=9-11&range=39-55)]
---
2017-09-29 04:13:03 +08:00
## Test the changes
2017-09-29 04:38:03 +08:00
Upon completion of the configuration changes, the property representing the primary key reflects the new data type. The following example demonstrates accessing the property in an MVC controller.
2017-09-29 04:13:03 +08:00
[!code-csharp[](identity/sample/src/ASPNET-IdentityDemo-PrimaryKeysConfig/Controllers/AccountController.cs?name=snippet_GetCurrentUserId&highlight=6)]