Asp.net core ASP.Net.Core.Identity 2.0上的FirstName和LastName以及其他字段
我对ASP.Net.Core.Identity不熟悉。我以前使用Net.core2.2推出了自己的安全性。我无法升级我的代码,因为大部分代码都无法运行,所以我决定进行切换。我所要做的就是向Asp.net core ASP.Net.Core.Identity 2.0上的FirstName和LastName以及其他字段,asp.net-core,asp.net-identity,Asp.net Core,Asp.net Identity,我对ASP.Net.Core.Identity不熟悉。我以前使用Net.core2.2推出了自己的安全性。我无法升级我的代码,因为大部分代码都无法运行,所以我决定进行切换。我所要做的就是向User添加新属性,这样我就可以执行以下操作:- User.FirstName //or User.Identity.FirstName //or some other syntax 我已经阅读了大量的文章并尝试了一些例子,但我什么也没有得到,这些例子要么不起作用,要么给我设计时错误,要么给我运行时错误 我已
User
添加新属性,这样我就可以执行以下操作:-
User.FirstName //or
User.Identity.FirstName
//or some other syntax
我已经阅读了大量的文章并尝试了一些例子,但我什么也没有得到,这些例子要么不起作用,要么给我设计时错误,要么给我运行时错误
我已经修改了如下的类。并更新了数据库。我可以在数据库中看到新字段。但我下一步该怎么办?走这么远很容易,但现在我完全卡住了
public partial class SystemUser : IdentityUser<int>
{
//public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Mobile { get; set; }
public string JobTitle { get; set; }
public string Hint { get; set; }
public int AddressId { get; set; }
public bool IsActive { get; set; }
//
// Summary:
// Gets or sets the date and time, in UTC, when any user lockout ends.
//
// Remarks:
// A value in the past means the user is not locked out.
[Column(TypeName = "datetime")]
public override DateTimeOffset? LockoutEnd { get; set; }
}
因为那根本不会带来什么。我似乎在每一个转折点都被挫败了:(
我正在使用VS2017,MySql,Pomelo.EntityFrameworkCore.MySql。非常感谢您的帮助。提前感谢
为了让这一切顺利进行,我必须做的是
- 删除存储的标识cookie
- 将数据从字符串更改为int(1、2、3等)
- 在数据库中,将ID列从string更改为int
它现在正在工作,但我不知道还有什么隐藏的宝石在等着我为了定制
标识用户
,没有必要将SystemUser
添加到DbContext
请遵循以下步骤:
SystemUser
public class SystemUser : IdentityUser<int>
{
public string FirstName { get; set; }
public string LastName { get; set; }
//your other properties
}
IdentityHostingStartup
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("ApplicationDbContextConnection")));
services.AddDefaultIdentity<SystemUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
});
}
}
公共类标识hostingstartup:IHostingStartup
{
公共void配置(IWebHostBuilder)
{
builder.ConfigureServices((上下文,服务)=>{
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“ApplicationDbContextConnection”);
services.AddDefaultIdentity()
.AddEntityFrameworkStores();
});
}
}
public class HomeController : Controller
{
private readonly ApplicationDbContext _context;
private readonly UserManager<SystemUser> _userManager;
public HomeController(ApplicationDbContext context
, UserManager<SystemUser> userManager)
{
_context = context;
_userManager = userManager;
}
[Authorize]
public async Task<IActionResult> Index()
{
SystemUser user = _context.Users.Where(s => s.UserName == User.Identity.Name).FirstOrDefault();
SystemUser user1 = await _userManager.FindByNameAsync(User.Identity.Name);
return View();
}
}
公共类HomeController:控制器
{
私有只读应用程序的bContext\u上下文;
私有只读用户管理器_UserManager;
公共HomeController(ApplicationDbContext上下文
,用户管理器(用户管理器)
{
_上下文=上下文;
_userManager=userManager;
}
[授权]
公共异步任务索引()
{
SystemUser user=_context.Users.Where(s=>s.UserName==user.Identity.Name).FirstOrDefault();
SystemUser user1=await\u userManager.FindByNameAsync(User.Identity.Name);
返回视图();
}
}
你已经试过了吗?是的。它不起作用:(你遇到了什么问题?我无法通过任何方式获取FirstName属性,也无法从数据库获取User.ID属性。我似乎看不到获取FirstName(或其他)属性的方法。你可能正在查找以下内容:
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("ApplicationDbContextConnection")));
services.AddDefaultIdentity<SystemUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
});
}
}
public class HomeController : Controller
{
private readonly ApplicationDbContext _context;
private readonly UserManager<SystemUser> _userManager;
public HomeController(ApplicationDbContext context
, UserManager<SystemUser> userManager)
{
_context = context;
_userManager = userManager;
}
[Authorize]
public async Task<IActionResult> Index()
{
SystemUser user = _context.Users.Where(s => s.UserName == User.Identity.Name).FirstOrDefault();
SystemUser user1 = await _userManager.FindByNameAsync(User.Identity.Name);
return View();
}
}