C# 使用Int键自定义Asp.net标识
我正在尝试使用C# 使用Int键自定义Asp.net标识,c#,asp.net,sql-server,asp.net-mvc,entity-framework,C#,Asp.net,Sql Server,Asp.net Mvc,Entity Framework,我正在尝试使用Int作为主键自定义Asp.net标识。我已经实现了将string键更改为int键的代码 using Microsoft.AspNet.Identity.EntityFramework; namespace NewAsp.netAuth.Models { public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
Int
作为主键自定义Asp.net标识。我已经实现了将string
键更改为int
键的代码
using Microsoft.AspNet.Identity.EntityFramework;
namespace NewAsp.netAuth.Models
{
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
}
public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public CustomUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
public CustomRoleStore(ApplicationDbContext context)
: base(context)
{
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
}
这是:
public AccountController(UserManager<ApplicationUser,int> userManager)
{
UserManager = userManager;
var userValidator = UserManager.UserValidator as UserValidator<ApplicationUser,int>;
userValidator.AllowOnlyAlphanumericUserNames = false;
}
public AccountController(UserManager)
您需要替换默认的数据访问实现
首先,您创建自己的用户类。它将具有整数主键、电子邮件作为用户名以及您想要的任何自定义字段
class User : IUser<int>
{
public int Id
{ get; set; }
public string Name
{ get; set; }
public string Email
{ get; set; }
public string PasswordHash
{ get; set; }
public string SecurityStamp
{ get; set; }
// other fields here
string IUser<int>.UserName
{
get { return Email; }
set { Email = value; }
}
}
class用户:IUser
{
公共整数Id
{get;set;}
公共字符串名
{get;set;}
公共字符串电子邮件
{get;set;}
公共字符串密码哈希
{get;set;}
公共字符串安全戳
{get;set;}
//这里还有其他领域
字符串IUser.UserName
{
获取{返回电子邮件;}
设置{Email=value;}
}
}
然后实现UserStore接口。可能还需要实现其他接口
class CustomUserStore:
IUserStore<User, int>,
IUserPasswordStore<User, int>,
IUserSecurityStampStore<User, int>
{
AppDbContext context = new AppDbContext();
public Task<User> FindByIdAsync(int userId)
{
return context.Users.FindAsync(userId);
}
public Task<User> FindByNameAsync(string userName)
{
return context.Users.FirstOrDefaultAsync(u => u.Email == userName);
}
public Task CreateAsync(User user)
{
context.Entry(user).State = EntityState.Added;
return context.SaveChangesAsync();
}
public Task UpdateAsync(User user)
{
context.Entry(user).State = EntityState.Modified;
return context.SaveChangesAsync();
}
public Task DeleteAsync(User user)
{
context.Entry(user).State = EntityState.Deleted;
return context.SaveChangesAsync();
}
public Task<bool> HasPasswordAsync(User user)
{
return Task.FromResult(user.PasswordHash != null);
}
public Task<string> GetPasswordHashAsync(User user)
{
return Task.FromResult(user.PasswordHash);
}
public Task SetPasswordHashAsync(User user, string passwordHash)
{
user.PasswordHash = passwordHash;
return Task.FromResult(0);
}
public Task<string> GetSecurityStampAsync(User user)
{
return Task.FromResult(user.SecurityStamp);
}
public Task SetSecurityStampAsync(User user, string stamp)
{
user.SecurityStamp = stamp;
return Task.FromResult(0);
}
public void Dispose()
{
context.Dispose();
}
}
class CustomUserStore:
Iuser商店,
IUserPasswordStore,
IUserSecurityStampStore
{
AppDbContext上下文=新的AppDbContext();
公共任务FindByIdAsync(int userId)
{
返回context.Users.FindAsync(userId);
}
公共任务FindByNameAsync(字符串用户名)
{
返回context.Users.FirstOrDefaultAsync(u=>u.Email==userName);
}
公共任务CreateAsync(用户)
{
context.Entry(user.State=EntityState.Added;
返回context.saveChangesSync();
}
公共任务更新同步(用户)
{
context.Entry(user.State=EntityState.Modified;
返回context.saveChangesSync();
}
公共任务DeleteAsync(用户)
{
context.Entry(user.State=EntityState.Deleted;
返回context.saveChangesSync();
}
公共任务HasPasswordAsync(用户)
{
返回Task.FromResult(user.PasswordHash!=null);
}
公共任务GetPasswordHashAsync(用户)
{
返回Task.FromResult(user.PasswordHash);
}
公共任务SetPasswordHashAsync(用户用户,字符串passwordHash)
{
user.PasswordHash=PasswordHash;
返回Task.FromResult(0);
}
公共任务GetSecurityStampAsync(用户)
{
返回Task.FromResult(user.SecurityStamp);
}
公共任务SetSecurityStampAsync(用户,字符串戳记)
{
user.SecurityStamp=stamp;
返回Task.FromResult(0);
}
公共空间处置()
{
context.Dispose();
}
}
将新的UserStore实现传递给UserManager:
class CustomUserManager : UserManager<User, int>
{
public CustomUserManager()
: base(new CustomUserStore())
{
}
}
类CustomUserManager:UserManager
{
公共CustomUserManager()
:base(新的CustomUserStore())
{
}
}
这会让你开始
class CustomUserManager : UserManager<User, int>
{
public CustomUserManager()
: base(new CustomUserStore())
{
}
}