c# – ASP.NET核心 – 自定义AspNetCore.Identity实现不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – ASP.NET核心 – 自定义AspNetCore.Identity实现不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4309字,纯文字阅读大概需要7分钟。
内容图文
我正在构建一个完全自定义的AspNetCore.Identity实现,因为我希望TKey全面成为System.Guid.尊敬的我,我已经派出类型……
>角色:IdentityRole< Guid,UserRole,RoleClaim>
> RoleClaim:IdentityRoleClaim< Guid>
>用户:IdentityUser< Guid,UserClaim,UserRole,UserLogin>
> UserClaim:IdentityUserClaim< Guid>
> UserLogin:IdentityUserLogin< Guid>
> UserRole:IdentityUserRole< Guid>
> UserToken:IdentityUserToken< Guid>
> ApplicationDbContext:IdentityDbContext< User,Role,Guid,UserClaim,UserRole,UserLogin,RoleClaim,UserToken>
> ApplicationRoleManager:RoleManager< Role>
> ApplicationRoleStore:RoleStore< Role,ApplicationDbContext,Guid,UserRole,RoleClaim>
> ApplicationSignInManager:SignInManager< User>
> ApplicationUserManager:UserManager< User>
> ** ApplicationUserStore **:UserStore< User,Role,ApplicationDbContext,Guid,UserClaim,UserRole,UserLogin,UserToken>
ApplicationUserStore是问题孩子!
履行
namespace NewCo.Identity
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
public sealed class Role : IdentityRole<Guid, UserRole, RoleClaim>
{
}
}
namespace NewCo.Identity
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
public sealed class UserRole : IdentityUserRole<Guid>
{
}
}
namespace NewCo.Identity
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
public sealed class RoleClaim : IdentityRoleClaim<Guid>
{
}
}
// The problem is here...
namespace NewCo.Identity
{
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
using System.Security.Claims;
public sealed class ApplicationUserStore : UserStore<User, Role, ApplicationDbContext, Guid, UserClaim, UserRole, UserLogin, UserToken>
{
}
}
错误
The type ‘NewCo.Identity.Role’ cannot be used as type parameter
‘TRole’ in the generic type or method ‘UserStore’. There
is no implicit reference conversion from ‘NewCo.Identity.Role’ to
‘Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>’.
据我所知,除非这是一些(co / contra / in)方差问题,否则所有代码都会检查出来……我出错了什么?
解决方法:
您的ApplicationUserStore也需要最终的RoleClaim(不要忘记更新相关的NuGet包,否则您不能使用这些新添加):
ApplicationUserStore : UserStore<
User, Role, ApplicationDbContext,
Guid, UserClaim, UserRole,
UserLogin, UserToken, RoleClaim>
另外,您的ApplicationRoleStore应该提供如何创建RoleClaim,
protected override RoleClaim CreateRoleClaim(Role role, Claim claim)
{
return new RoleClaim
{
RoleId = role.Id,
ClaimType = claim.Type,
ClaimValue = claim.Value
};
}
此外,ApplicationUserStore也应该提供这些映射:
protected override UserClaim CreateUserClaim(User user, Claim claim)
{
var userClaim = new UserClaim { UserId = user.Id };
userClaim.InitializeFromClaim(claim);
return userClaim;
}
protected override UserLogin CreateUserLogin(User user, UserLoginInfo login)
{
return new UserLogin
{
UserId = user.Id,
ProviderKey = login.ProviderKey,
LoginProvider = login.LoginProvider,
ProviderDisplayName = login.ProviderDisplayName
};
}
protected override UserRole CreateUserRole(User user, Role role)
{
return new UserRole
{
UserId = user.Id,
RoleId = role.Id
};
}
protected override UserToken CreateUserToken(User user, string loginProvider, string name, string value)
{
return new UserToken
{
UserId = user.Id,
LoginProvider = loginProvider,
Name = name,
Value = value
};
}
然后将内置服务重定向到您的自定义服务:
services.AddScoped<UserStore<User, Role, ApplicationDbContext, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>, ApplicationUserStore>();
services.AddScoped<UserManager<User>, ApplicationUserManager>();
services.AddScoped<RoleManager<Role>, ApplicationRoleManager>();
services.AddScoped<SignInManager<User>, ApplicationSignInManager>();
services.AddScoped<RoleStore<Role, ApplicationDbContext, int, UserRole, RoleClaim>, ApplicationRoleStore>();
services.AddScoped<IEmailSender, AuthMessageSender>();
services.AddScoped<ISmsSender, AuthMessageSender>();
现在介绍您的自定义服务:
services.AddIdentity<User, Role>(identityOptions =>
{
// ...
}).AddUserStore<ApplicationUserStore>()
.AddUserManager<ApplicationUserManager>()
.AddRoleStore<ApplicationRoleStore>()
.AddRoleManager<ApplicationRoleManager>()
.AddSignInManager<ApplicationSignInManager>()
// You **cannot** use .AddEntityFrameworkStores() when you customize everything
//.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders();
内容总结
以上是互联网集市为您收集整理的c# – ASP.NET核心 – 自定义AspNetCore.Identity实现不起作用全部内容,希望文章能够帮你解决c# – ASP.NET核心 – 自定义AspNetCore.Identity实现不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。