如何将 role/claims 动态添加到 Asp.net5/EF7 中的 Asp.Net 身份表?
How do I add a role/claims dynamically to the Asp.Net identity tables in Asp.net5/EF7?
我正在尝试创建 WebAPI 服务以使用 Asp.Net5/EntityFramework7 中的 Asp.Net 身份提供安全性。我使用 Asp.Net 5 个模板创建了一个 Web 应用程序项目。它为我提供了管理用户的模板代码。现在我正在尝试为 roles/claims 做同样的事情。
我创建了一个 ApplicationRole class 继承了 IdentityRole。
public class ApplicationRole: IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name, string description) : base(name)
{
this.Description = description;
}
public virtual string Description { get; set; }
}
My Web API controller class 添加新角色的方法如下:
[HttpPost("CreateRole", Name = "CreateRole")]
public async Task CreateRole([FromBody]string role)
{
var applicationRole = new ApplicationRole(role, role);
var idResult = await _roleManager.CreateAsync(applicationRole);
if (idResult.Succeeded)
{
_logger.LogInformation(3, "Role Created successfully");
}
else
{
var resp = new HttpResponseMessage()
{
Content = new StringContent("Internal error occurred"),
ReasonPhrase = "Internal error occurred",
StatusCode = HttpStatusCode.BadRequest
};
throw new HttpResponseException(resp);
}
}
当我尝试执行此代码时,应用程序崩溃并出现异常
"A database operation failed while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: entityType
There are pending model changes for ApplicationDbContext
Scaffold a new migration for these changes and apply them to the database from the command line:
> dnx ef migrations add [migration name]
> dnx ef database update
"
它在 var idResult = await _roleManager.CreateAsync(applicationRole) 行崩溃了;
applicationRole 对象变量的快速监视值可以在 attached snapshot 中找到。执行上述行时,控件转到 ApplicationDbContext class 并执行 BuildModel() 方法。我没有对 AspNetRoles 表进行任何更改,但应用程序仍将我指向 运行 迁移脚本,有人可以帮我解决这个问题吗?如何将 role/claims 动态添加到 Asp.net5/EntityFramework7 中的 Asp.Net 身份表?
注意:在 运行 期间的 RoleManager 对象中,我看到错误消息为 "Value cannot be null.\r\nParameter name: entityType",请查找 attached snapshot
我找到了这个问题的解决方案。我还需要在 ApplicationDbContext class 中添加 ApplicationRole。这解决了问题。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
...
...
}
我从 post How do I extend IdentityRole using Web API 2 + AspNet Identity 2
得到了解决方案
我正在尝试创建 WebAPI 服务以使用 Asp.Net5/EntityFramework7 中的 Asp.Net 身份提供安全性。我使用 Asp.Net 5 个模板创建了一个 Web 应用程序项目。它为我提供了管理用户的模板代码。现在我正在尝试为 roles/claims 做同样的事情。
我创建了一个 ApplicationRole class 继承了 IdentityRole。
public class ApplicationRole: IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name, string description) : base(name)
{
this.Description = description;
}
public virtual string Description { get; set; }
}
My Web API controller class 添加新角色的方法如下:
[HttpPost("CreateRole", Name = "CreateRole")]
public async Task CreateRole([FromBody]string role)
{
var applicationRole = new ApplicationRole(role, role);
var idResult = await _roleManager.CreateAsync(applicationRole);
if (idResult.Succeeded)
{
_logger.LogInformation(3, "Role Created successfully");
}
else
{
var resp = new HttpResponseMessage()
{
Content = new StringContent("Internal error occurred"),
ReasonPhrase = "Internal error occurred",
StatusCode = HttpStatusCode.BadRequest
};
throw new HttpResponseException(resp);
}
}
当我尝试执行此代码时,应用程序崩溃并出现异常
"A database operation failed while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: entityType
There are pending model changes for ApplicationDbContext
Scaffold a new migration for these changes and apply them to the database from the command line:
> dnx ef migrations add [migration name]
> dnx ef database update
"
它在 var idResult = await _roleManager.CreateAsync(applicationRole) 行崩溃了; applicationRole 对象变量的快速监视值可以在 attached snapshot 中找到。执行上述行时,控件转到 ApplicationDbContext class 并执行 BuildModel() 方法。我没有对 AspNetRoles 表进行任何更改,但应用程序仍将我指向 运行 迁移脚本,有人可以帮我解决这个问题吗?如何将 role/claims 动态添加到 Asp.net5/EntityFramework7 中的 Asp.Net 身份表?
注意:在 运行 期间的 RoleManager 对象中,我看到错误消息为 "Value cannot be null.\r\nParameter name: entityType",请查找 attached snapshot
我找到了这个问题的解决方案。我还需要在 ApplicationDbContext class 中添加 ApplicationRole。这解决了问题。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
...
...
}
我从 post How do I extend IdentityRole using Web API 2 + AspNet Identity 2
得到了解决方案