AspNetCore Identity用户缺少ApplicationUser自定义属性
创始人
2024-09-20 07:30:14
0

当使用AspNetCore Identity时,你可以通过继承IdentityUser类来为用户添加自定义属性。以下是一种解决方法的代码示例:

  1. 创建一个继承自IdentityUser的自定义用户类ApplicationUser,并在其中添加你需要的自定义属性。
using Microsoft.AspNetCore.Identity;

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // 添加其他自定义属性
}
  1. Startup.cs文件中的ConfigureServices方法中,将IdentityUser替换为ApplicationUser
public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddIdentity()
        .AddEntityFrameworkStores()
        .AddDefaultTokenProviders();

    // ...
}
  1. DbContext类(通常是ApplicationDbContext)中,将IdentityUser替换为ApplicationUser
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext : IdentityDbContext
{
    // ...
}

现在,你可以使用ApplicationUser类来访问自定义的属性了。例如:

public class SomeController : Controller
{
    private readonly UserManager _userManager;

    public SomeController(UserManager userManager)
    {
        _userManager = userManager;
    }

    public async Task SomeAction()
    {
        var userId = User.GetUserId();
        var user = await _userManager.FindByIdAsync(userId);

        var firstName = user.FirstName;
        var lastName = user.LastName;

        // ...

        return View();
    }
}

以上示例中,ApplicationUser类添加了FirstNameLastName属性,并且在SomeController中使用UserManager来获取当前用户的信息,并访问自定义属性。

相关内容

热门资讯

透视线上"hhpok... 透视线上"hhpoker真能买到挂吗"详细辅助AI教程(透视脚本免费app软件),支持语音通讯、好友...
透视插件"wepok... 1、透视插件"wepoker怎么开辅助"详细辅助辅助教程(辅助器下载攻略);详细教程。2、wepok...
透视透视挂"菠萝德普... 1、透视透视挂"菠萝德普辅助器免费版在哪里"详细辅助科技教程(私人定制透视技巧)。2、菠萝德普辅助器...
透视总结"wepok... 透视总结"wepoker免费脚本咨询"详细辅助wepoke教程(辅助真的假的技巧)准备好在wepok...
透视app"poke... 透视app"pokernow辅助工具"详细辅助揭秘教程(辅助器技巧)关于pokernow辅助工具机制...
透视黑科技"hhpo... 透视黑科技"hhpoker辅助器"详细辅助系统教程(辅助器免费教程);hhpoker辅助器软件透明挂...
透视线上"hhpok... 透视线上"hhpoker透视方法"详细辅助揭秘攻略(俱乐部辅助透视)是一款可以让一直输的玩家,快速成...
透视存在"werpl... 透视存在"werplan辅助软件"详细辅助玩家教你(私人局外卦攻略),支持语音通讯、好友开房及战队组...
透视系统"wepok... 透视系统"wepoker破解游戏盒子"详细辅助必备教程(作弊辅助攻略);wepoker破解游戏盒子软...
透视软件"wepok... 透视软件"wepoker永久免费脚本"详细辅助存在挂教程(俱乐部辅助攻略)1、不需要AI权限,帮助你...