ค้นหาแพ็คเกจ NUGET ที่เกี่ยวข้องสำหรับ DBMS ของคุณ (เช่น: identity.dapper.sqlserver)
ในการกำหนดค่าการเชื่อมต่อ DBMS คุณสามารถเพิ่มส่วน dapperidentity และส่วน dapperidentityCryptography ลงในไฟล์การกำหนดค่าของคุณเช่นนี้:
"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" : {
"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 สำหรับ 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 Core สำหรับการใช้งาน System.Data
ระบุ
services . AddIdentity < DapperIdentityUser < Guid > , DapperIdentityRole < Guid > > ( )
. AddDapperIdentityFor < T , Guid > ( ) ; ผ่านคลาสที่กำหนดเองที่สืบทอดมาจาก SqlServerConfiguration (หรืออื่น ๆ )
public class CustomSqlServerConfiguration : SqlServerConfiguration
{
public CustomSqlServerConfiguration ( )
{
base . SchemaName = "[customSchema]" ;
}
}และเพิ่มด้วย
services . AddDapperIdentityFor < CustomSqlServerConfiguration > ( )