我无法从Identity扩展属性。
这样做的全部原因是让我的员工数据库与应用程序数据库(包括身份资料)集成,并将其用作一个大数据库。
我尝试了这个answer,但似乎他们正在使用另一个版本的ASP.NET。我使用的是ASP.NET Core,版本:2.0.3
以下是我的ApplicationUser.cs
文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
namespace src.Models
{
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim("FirstName", this.FirstName.ToString()));
userIdentity.AddClaim(new Claim("LastName", this.LastName.ToString()));
userIdentity.AddClaim(new Claim("JobTitle", this.JobTitle.ToString()));
userIdentity.AddClaim(new Claim("Status", this.Status.ToString()));
return userIdentity;
}
string FirstName { get; set; }
string LastName { get; set; }
string JobTitle { get; set; }
string Status { get; set; }
int Age { get; set; }
}
}
我在 CreateIdentityAsync
上收到错误,错误是:
'UserManager<ApplicationUser>' does not contain a definition for 'CreateIdentityAsync'
and no extension method 'CreateIdentityAsync' accepting a first argument of type
'UserManager<ApplicationUser>' could be found (are you missing a using directive
or an assembly reference?) [src]
错误 DefaultAuthenticationTypes
,错误:
The name 'DefaultAuthenticationTypes' does not exist in the current context [src]
这对ASP.NET Core来说是不可能的,还是我做错了什么?
答案 0 :(得分:0)
我自己想通了。 ASP.NET Core无法实现我的目标。我不得不做别的事。
我创建了一个AppClaimsPrincipalFactory.cs文件
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
public AppClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager
, RoleManager<IdentityRole> roleManager
, IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor)
{ }
public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
if (!string.IsNullOrWhiteSpace(user.FirstName))
{
((ClaimsIdentity)principal.Identity).AddClaims(new[] {
new Claim(ClaimTypes.GivenName, user.FirstName)
});
}
if (!string.IsNullOrWhiteSpace(user.LastName))
{
((ClaimsIdentity)principal.Identity).AddClaims(new[] {
new Claim(ClaimTypes.Surname, user.LastName),
});
}
return principal;
}
}
将此行添加到我的Startup.cs文件
services.AddScoped<Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
答案 1 :(得分:-1)
将原始解决方案添加到@remi https://adrientorris.github.io/aspnet-core/identity/extend-user-model.html
public class ApplicationUser : IdentityUser {
public string FirstName { get; set; }
public string LastName { get; set; }
}
创建委托人工厂
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole> {
public AppClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager
, RoleManager<IdentityRole> roleManager
, IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor)
{ }
public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
if (!string.IsNullOrWhiteSpace(user.FirstName))
{
((ClaimsIdentity)principal.Identity).AddClaims(new[] {
new Claim(ClaimTypes.GivenName, user.FirstName)
});
}
if (!string.IsNullOrWhiteSpace(user.LastName))
{
((ClaimsIdentity)principal.Identity).AddClaims(new[] {
new Claim(ClaimTypes.Surname, user.LastName),
});
}
return principal;
}
}
添加启动类
public void ConfigureServices(IServiceCollection services)
{
// [...]
services.AddDbContext<SecurityDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Security")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<SecurityDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
// [...]
}