我试图在ASP.NET核心标识系统中添加一个新角色。但到目前为止还没有运气。
代码: C#
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
private ApplicationDbContext _context;
var roleStore = new RoleStore<IdentityRole>(_context);
var roleManager = new RoleManager<IdentityRole>(role
在我的项目中,我将ASP.NET标识中的字符串键更改为int键。当我们想通过字符串键创建一个角色时,我们可以编写
public class AppRole : IdentityRole {
public AppRole() : base() { }
public AppRole(string name) : base(name) { }
}
//IdentityRole class is for ASP.NET Identity
public class IdentityRole : IdentityRole<string, IdentityUserRole>
我正在使用ASP.NET标识和Web进行身份验证。
我有这样的代码:
public async Task<IdentityResult> RegisterUser(UserModel userModel)
{
IdentityUser user = new IdentityUser
{
UserName = userModel.UserName,
};
var result = await _userManager.CreateAsync(user, userModel.Password);
我尝试在我的IdentityDataContext类中使用protected override void OnModelCreating(ModelBuilder builder),并创建一个迁移来播种这些数据: protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if need
我使用的是ASP.Net核心3.1。在我的IdentityServer中,我像这样添加用户和角色
public static void EnsureSeedData(IServiceProvider serviceProvider)
{
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManage
我想让所有用户的角色按他们的名字分组,还包括没有任何角色的用户。
我正在使用asp.net identity 2,到目前为止我已经尝试过
from user in users
from identityUserRole in user.Roles
join identityRole in identityRole on identityUserRole.RoleId equals identityRole.Id into r
from roles in r
group roles.Name by user.UserName into g
select new
{
UserName =
通过查看这里的文章,我看到了通过实体框架种子创建ASP.NET标识角色的两种不同方法。一种使用RoleManager,另一种使用RoleStore。我想知道这两者是否有区别。由于使用后者将避免少初始化一次。
string[] roles = { "Admin", "Moderator", "User" };
// Create Role through RoleManager
var roleStore = new RoleStore<IdentityRole>(context);
var manager = new RoleMa
在ASP.NET核心应用程序中,我希望创建某些角色作为管理不同用户权限的基础。可悲的是,文档通知详细说明了如何在控制器/操作中使用自定义角色,但没有详细说明如何创建它们。我发现我可以使用RoleManager<IdentityRole>来解决这个问题,当实例的定义和ASP.NET核心标识在应用程序中注册时,它会自动注入到控制器构造函数中。
这让我可以添加一个自定义角色,如下所示:
var testRole = new IdentityRole("TestRole");
if(!roleManager.RoleExistsAsync(testRole.Name).Re
问题的上下文
在我的web应用程序中,我一直在使用ASP.NET标识。我从IdentityUser继承了用户类,从而创建了自定义用户配置文件。为了能够将角色与例如角色组连接起来,我还在我的数据库中创建了一个自定义角色类,该类继承自IdentityRole类。
我的数据库模型如下所示:
问题不在于创建一个用户。这只会起作用:
var userManager = new UserManager<User>(new UserStore<User>(_context));
var result = userManager.Create(user, password);
创
我试图在我的UserRole核心MVC应用程序中播种ASP.NET,但是它抛出了下面的异常。
引发的异常:“'Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.AspNetRoleManager1Microsoft.AspNetCore.Identity.IdentityRole'.”中的“System.InvalidO
根据MSDN:
如果您的场景包括以下任何一项内容,建议使用上一节中描述的自定义初始化逻辑:
需要调用外部API的数据,如ASP.NET核心标识角色和用户创建
但是当我使用
var roleBuilder = modelBuilder.Entity<IdentityRole>();
.HasData(
new IdentityRole
{
Id = "2c5e174e-3b0e-446f-86af-483d56fd7210"
我在Asp.Net Web 2中开发了一种身份验证机制,该机制具有授予刷新令牌的功能,该机制基于关于的教程
这是我的问题。假设以下场景:用户使用密码登录并获得刷新令牌和访问令牌。访问令牌实际上包括了他的角色(因此他的权限在应用程序中)。同时,系统管理员将更改这个人的角色,因此一旦他的访问令牌过期,并且他想使用刷新令牌来获得一个新的访问令牌,他的新访问令牌必须包括为他更新的新角色。
在我的"RefreshTokenProvider“类中,我使用"GrantResourceOwnerCredentials”方法中的以下代码从数据库中获取用户角色并将它们添加到声明中:
var rol
我试图在Asp.Net 5/EntityFramework7 7中创建WebAPI服务,以便使用Asp.Net标识来提供安全性。我使用Asp.Net 5模板创建了一个web应用程序项目。它为我提供了管理用户的模板代码。现在,我正试图对角色/声明做同样的事情。
我已经创建了一个继承ApplicationRole的IdentityRole类。
public class ApplicationRole: IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(st
这是角色的控制器,问题是如何创建表单,从窗体或视图中插入名为"Admins“的角色。提前感谢
public ActionResult CreateRole()
{
string Output = "";
ApplicationDbContext db = new ApplicationDbContext();
RoleManager<IdentityRole> RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
根据Microsoft文档,我试图在Asp.Net核心2.1中实现基于角色的授权。
下面是我的Startup.cs的样子:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this meth
我一直在阅读dotnet核心身份验证文档,并试图做一些示例。我能够创建新用户,但在创建新角色时遇到了问题。当我调用RoleManager.CreateAsync时,它不会创建新角色。
ApplicationContext类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<string>, string>
{
public ApplicationDbContext(DbContextOptions<Applicatio
我有一个问题,我是新的身份,但我仍然想知道什么是正确的方式分配角色的用户时,他正在注册?
我这里有个密码:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName =
在我的应用程序中,我已经将用户和一些数据植入到我的应用程序中,但是现在我正在尝试创建一个系统,在这个系统中,我可以为用户分配一个角色。我认为这可以通过使用ASP.NET标识中的索赔系统或使用角色名称的基于请求的授权系统来实现,我对采取哪种方法以及如何实现感到有点困惑。
目前,我已经创建了一种种子用户和种子联系人的方法,但是我读到了关于RoleManager<IdentityRole>的文章,我认为这是正确的方法,但我还是不确定。
public static class DbInit
{
public static void SeedDb(ApplicationDbCo
我使用带有MVC6标识的Asp.net项目,并希望将ID列从字符串更改为INT。我跟随了这篇文章
我得到一个错误,可以将null插入到ID列中,用于角色和用户,但是如果我恢复到规范,它就能工作了。
public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Applicat
我在ASP.Net MVC 5中使用个人身份验证。
我在上下文中为"Admin“用户添加了密码"12345678”,并且它的角色为“所有者”。如这里所示:
if (!context.Roles.Any(r => r.Name == "Owner"))
{
var rolestore = new RoleStore<IdentityRole>(context);
var rolemanager = new RoleManager<IdentityRole>(rolestore);
var role = new
我正在准备ASP.NET核心(实体框架)中的应用程序,我希望有一些功能仅限于特定的用户。
为了在Startup.cs中实现这一点,我添加了以下代码服务:
services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>
().AddEntityFrameworkStores<ApplicationDbContext>();
类来创建新的默认用户:
public static class MyIdentityDataInitializer
{
public static void S