Skip to content

Latest commit

 

History

History
173 lines (121 loc) · 6.71 KB

Configure-To-Existing-Identity.md

File metadata and controls

173 lines (121 loc) · 6.71 KB

How to configure DbContext for ASP.NET Core Identity

  • You can setup the DbContext - in Startup.cs:
services.AddAdminAspNetIdentityServices<AdminDbContext, UserDto<int>, int, RoleDto<int>, int, int, int,
                                UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole,
                                UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>();
  • Method AddAdminAspNetIdentityServices expects the generic param TAdminDbContext that inherits from IdentityDbContext and implements interface IAdminPersistedGrantIdentityDbContext (for operation data connected with Asp.Net Core Identity)

How to configure Identity primary key data type in ASP.NET Core Identity

  • By default, it's used int as the primary key, but you can change to Guid or string.

How to use for example Guid:

1. Change int to Guid in Startup.cs:

Original:

services.AddAdminAspNetIdentityServices<AdminDbContext, UserDto<int>, int, RoleDto<int>, int, int, int,
                                UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole,
                                UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>();

New:

services.AddAdminAspNetIdentityServices<AdminDbContext, UserDto<Guid>, Guid, RoleDto<Guid>, Guid, Guid, Guid,
                                UserIdentity, UserIdentityRole, Guid, UserIdentityUserClaim, UserIdentityUserRole,
                                UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>();

2. Change int to Guid in all files in folder - Skoruba.IdentityServer4.Admin.EntityFramework/Entities/Identity:

For example - UserIdentity.cs:

Original:

public class UserIdentity : IdentityUser<int>
	{

	}

New:

public class UserIdentity : IdentityUser<Guid>
	{

	}
  • Change int to Guid in other files in this folder - Skoruba.IdentityServer4.Admin.EntityFramework/Entities/Identity

3. Change int to Guid in all files in folder - Skoruba.IdentityServer4.Admin/Views/Identity:

For example - Role.cshtml:

Original:

@model Skoruba.IdentityServer4.Admin.BusinessLogic.Dtos.Identity.RoleDto<int>

...

@if (!EqualityComparer<int>.Default.Equals(Model.Id, default(int)))

...

New:

@model Skoruba.IdentityServer4.Admin.BusinessLogic.Dtos.Identity.RoleDto<Guid>

...

@if (!EqualityComparer<Guid>.Default.Equals(Model.Id, default(Guid)))

...
  • Change int to Guid in other files in this folder - Skoruba.IdentityServer4.Admin/Views/Identity

4. Change int to Guid in AdminDbContext - Skoruba.IdentityServer4.Admin.EntityFramework/DbContexts:

Original:

public class AdminDbContext : IdentityDbContext<UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>,
        IAdminConfigurationDbContext, IAdminLogDbContext, IAdminPersistedGrantIdentityDbContext

New:

public class AdminDbContext : IdentityDbContext<UserIdentity, UserIdentityRole, Guid, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>,
        IAdminConfigurationDbContext, IAdminLogDbContext, IAdminPersistedGrantIdentityDbContext

5. Change int to Guid in GrantController - Skoruba.IdentityServer4.Admin/Controllers:

Original:

public class GrantController : BaseController
    {
        private readonly IPersistedGrantService<AdminDbContext, UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken> _persistedGrantService;
        private readonly IStringLocalizer<GrantController> _localizer;

        public GrantController(IPersistedGrantService<AdminDbContext, UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken> persistedGrantService,
            ILogger<ConfigurationController> logger,
            IStringLocalizer<GrantController> localizer) : base(logger)
        {
            _persistedGrantService = persistedGrantService;
            _localizer = localizer;
        }
    }

New:

public class GrantController : BaseController
    {
        private readonly IPersistedGrantService<AdminDbContext, UserIdentity, UserIdentityRole, Guid, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken> _persistedGrantService;
        private readonly IStringLocalizer<GrantController> _localizer;

        public GrantController(IPersistedGrantService<AdminDbContext, UserIdentity, UserIdentityRole, Guid, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken> persistedGrantService,
            ILogger<ConfigurationController> logger,
            IStringLocalizer<GrantController> localizer) : base(logger)
        {
            _persistedGrantService = persistedGrantService;
            _localizer = localizer;
        }
    }

6. Change int to Guid in IdentityController - Skoruba.IdentityServer4.Admin/Controllers:

Original:

public class IdentityController : BaseIdentityController<AdminDbContext, UserDto<int>, int, RoleDto<int>, int, int, int, UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>
    {
        public IdentityController(IIdentityService<AdminDbContext, UserDto<int>, int, RoleDto<int>, int, int, int, UserIdentity, UserIdentityRole, int, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken> identityService, ILogger<ConfigurationController> logger, IStringLocalizer<IdentityController> localizer)
            : base(identityService, logger, localizer)
        {
        }
    }

New:

public class IdentityController : BaseIdentityController<AdminDbContext, UserDto<Guid>, Guid, RoleDto<Guid>, Guid, Guid, Guid, UserIdentity, UserIdentityRole, Guid, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>
    {
        public IdentityController(IIdentityService<AdminDbContext, UserDto<Guid>, Guid, RoleDto<Guid>, Guid, Guid, Guid, UserIdentity, UserIdentityRole, Guid, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken> identityService, ILogger<ConfigurationController> logger, IStringLocalizer<IdentityController> localizer)
            : base(identityService, logger, localizer)
        {
        }
    }