Найдите соответствующий пакет Nuget для вашего СУБД (например: identity.dapper.sqlserver).
Для настройки соединения СУБД, вы можете добавить Dapperidentity и раздел DapperientityCryptography в свой файл конфигурации, как это:
"DapperIdentity" : {
"ConnectionString" : " Connection string of your database " ,
"Username" : " user " ,
"Password" : " 123 "
},
"DapperIdentityCryptography" : {
"Key" : " Base64 32 bytes key " ,
"IV" : " Base64 16 bytes key "
} Пример:
Ключ: «E546C8DF278CD5931069B522E695D4F2» (32 байта)
BASE64 Кодированный ключ: «rtu0nkm4reyynzhdrdu5mzewnjlcntiyrty5nuq0rji ="
IV: "somereallycooliv" (16 байтов)
BASE64 IV: "U29TZVJLYWXSEUNVB2XJVG =="
В качестве альтернативы, вы можете использовать раздел ConnectionStrings по умолчанию:
"ConnectionStrings" : {
"DefaultConnection" : " Connection string of your database "
}Или вы можете использовать команды пользовательских секретов:
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"
Dapperidentity: пароль может быть зашифрован с помощью AES256, используя предоставленную ключ и IV.
В файле startup.cs перейдите в ConfigureServices и добавьте следующие строки:
services . ConfigureDapperConnectionProvider < T > ( Configuration . GetSection ( "DapperIdentity" ) )
. ConfigureDapperIdentityCryptography ( Configuration . GetSection ( "DapperIdentityCryptography" ) ) ;
services . AddIdentity < DapperIdentityUser , DapperIdentityRole < int > > ( )
. AddDapperIdentityFor < T > ( )
. AddDefaultTokenProviders ( ) ;или
services . ConfigureDapperConnectionProvider < T > ( Configuration . GetSection ( "ConnectionStrings" ) ) Где t для метода ConfigureDapperConnectionProvider - это DBMSNameConnectionProvider (например: SqlServerConnectionProvider ) и t для метода AddDapperIdentityFor for - DBMSNameConfiguration (например, SqlServerConfiguration ).
Если вы хотите использовать транзакции ко всем методам идентичности, вам придется добавить .ConfigureDapperIdentityOptions(new DapperIdentityOptions { UseTransactionalBehavior = true }) ниже ConfigureDapperIdentityCryptography(Configuration.GetSection("DapperIdentityCryptography"));
А внутри вашего контроллера вам придется вставить конструктор 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 > ;
} И после всех операций вам придется вызвать метод DapperUserStore.SaveChanges() , в противном случае ваши изменения будут откачены.
В настоящее время поддерживаются только SQL Server, PostgreSQL и MySQL. Мы планируем поддержку Oracle, когда компания выпустит основную версию .NET для своей реализации System.Data.
Укажите
services . AddIdentity < DapperIdentityUser < Guid > , DapperIdentityRole < Guid > > ( )
. AddDapperIdentityFor < T , Guid > ( ) ; Пройти пользовательский класс, который наследует от SqlServerConfiguration (или другого)
public class CustomSqlServerConfiguration : SqlServerConfiguration
{
public CustomSqlServerConfiguration ( )
{
base . SchemaName = "[customSchema]" ;
}
}И добавить это с
services . AddDapperIdentityFor < CustomSqlServerConfiguration > ( )