Encontre o pacote NUGET correspondente para o seu DBMS (por exemplo: identity.dapper.sqlserver).
Para configurar a conexão DBMS, você pode adicionar uma seção DapperIdentity e uma DapperIdentityCriptografia ao seu arquivo de configuração como este:
"DapperIdentity" : {
"ConnectionString" : " Connection string of your database " ,
"Username" : " user " ,
"Password" : " 123 "
},
"DapperIdentityCryptography" : {
"Key" : " Base64 32 bytes key " ,
"IV" : " Base64 16 bytes key "
} Exemplo:
Chave: "E546C8DF278CD5931069B522E695D4F2" (32 bytes)
Chave codificada base64: "rtu0nkm4reyynzhdrdu5mzewnjlcntiyrty5nuq0rji ="
IV: "Somereallycooliv" (16 bytes)
Base64 codificado IV: "u29tzvjlywxseunvb2xjvg =="
Como alternativa, você pode usar a seção padrão do ConnectionsTrings :
"ConnectionStrings" : {
"DefaultConnection" : " Connection string of your database "
}Ou você pode usar os comandos de segredos do usuário:
dotnet user-secrets set DapperIdentity:ConnectionString "Connection string of your database"
dotnet user-secrets set DapperIdentity:Password "123"
dotnet user-secrets set DapperIdentity:Username "user"
dotnet user-secrets set DapperIdentityCryptography:Key "Base64 32 bytes key"
dotnet user-secrets set DapperIdentityCryptography:IV "Base64 16 bytes key"
O DapperIdentity: a senha pode ser criptografada com o AES256 usando a chave e o IV fornecido.
No arquivo startup.cs , vá para o ConfigureServices e adicione as seguintes linhas:
services . ConfigureDapperConnectionProvider < T > ( Configuration . GetSection ( "DapperIdentity" ) )
. ConfigureDapperIdentityCryptography ( Configuration . GetSection ( "DapperIdentityCryptography" ) ) ;
services . AddIdentity < DapperIdentityUser , DapperIdentityRole < int > > ( )
. AddDapperIdentityFor < T > ( )
. AddDefaultTokenProviders ( ) ;ou
services . ConfigureDapperConnectionProvider < T > ( Configuration . GetSection ( "ConnectionStrings" ) ) Onde t para o método ConfigureDapperConnectionProvider é DBMSNameConnectionProvider (por exemplo: SqlServerConnectionProvider ) e t para o método AddDapperIdentityFor para é DBMSNameConfiguration (por exemplo: SqlServerConfiguration ).
Se você deseja usar transações para todos os métodos de identidade, precisará adicionar .ConfigureDapperIdentityOptions(new DapperIdentityOptions { UseTransactionalBehavior = true }) abaixo ConfigureDapperIdentityCryptography(Configuration.GetSection("DapperIdentityCryptography"));
E dentro do seu controlador, você terá que inserir o construtor um DapperUserStore<TUser, TKey, TUserRole, TRoleClaim, TUserClaim, TUserLogin, TRole>
private readonly DapperUserStore < CustomUser , int , DapperIdentityUserRole < int > , DapperIdentityRoleClaim < int > , DapperIdentityUserClaim < int > , DapperIdentityUserLogin < int > , CustomRole > _dapperStore ;
.. .
public ManageController ( IUserStore < CustomUser > dapperStore )
{
_dapperStore = dapperStore as DapperUserStore < CustomUser , int , DapperIdentityUserRole < int > , DapperIdentityRoleClaim < int > , DapperIdentityUserClaim < int > , DapperIdentityUserLogin < int > , CustomRole > ;
} E depois de todas as operações, você terá que ligar para o método DapperUserStore.SaveChanges() , caso contrário, suas alterações serão revertidas.
Atualmente, apenas o SQL Server, o PostgreSQL e o MySQL são suportados. Planejamos o suporte para a Oracle quando a empresa lançar a versão .NET Core para sua implementação do System.Data.
Especifique o
services . AddIdentity < DapperIdentityUser < Guid > , DapperIdentityRole < Guid > > ( )
. AddDapperIdentityFor < T , Guid > ( ) ; Passe uma classe personalizada que herda do SqlServerConfiguration (ou outro)
public class CustomSqlServerConfiguration : SqlServerConfiguration
{
public CustomSqlServerConfiguration ( )
{
base . SchemaName = "[customSchema]" ;
}
}E adicione com
services . AddDapperIdentityFor < CustomSqlServerConfiguration > ( )