ASP.NET Core 8 Web API 终结点不包含我的自定义字段

ASP.NET Core 8 Web API endpoint does not contain my custom fields

提问人:Allen Prado 提问时间:11/17/2023 最后编辑:marc_sAllen Prado 更新时间:11/21/2023 访问量:62

问:

我创建了一个实体,并成功地将其扩展到数据库,迁移在数据库中创建了它。ApplicationUserIdentityUser

但是当我转到注册端点时,它没有显示要注册的自定义字段:

enter image description here

enter image description here

实体:ApplicationUser

public class ApplicationUser : IdentityUser
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public int FavoriteTeam { get; set; }
    public string? PhotoPath { get; set; }
    public string? FirstLast { get; set; }
}

DbContext:

public class ContextBase : IdentityDbContext<ApplicationUser>
{
    public ContextBase(DbContextOptions<ContextBase> options) : base(options)
    {
    }
}

程序

builder.Services.AddIdentityApiEndpoints<ApplicationUser>()
    .AddEntityFrameworkStores<ContextBase>();
     
-----

app.MapIdentityApi<ApplicationUser>();
asp.net-core-webapi 标识 net-8.0 自定义属性

评论


答:

0赞 Jalpa Panchal 11/17/2023 #1

如果处理注册的 API 端点未配置为接受 .你能试试下面的代码吗?ApplicationUser

public class RegisterDto
{
    public string Email { get; set; }
    public string Password { get; set; }

    // Custom fields
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int FavoriteTeam { get; set; }
    public string PhotoPath { get; set; }
}

更新注册终结点

public async Task<IActionResult> Register(RegisterDto model)
{
    var user = new ApplicationUser
    {
        UserName = model.Email,
        Email = model.Email,
        FirstName = model.FirstName,
        LastName = model.LastName,
        FavoriteTeam = model.FavoriteTeam,
        PhotoPath = model.PhotoPath
    };

    // other registration process
}

评论

0赞 Allen Prado 11/17/2023
嗨,伙计,感谢您的回答,但我无法访问注册端点,它是身份,可能有办法在我的程序中覆盖它 .cs
0赞 Abdullah Etizaz 11/21/2023 #2

下面是编写自定义标识注册终结点的一种方法。此终结点采用 RegisterDTO 并将其映射到 User 对象。

[HttpPost("CustomRegister")]
public async Task<Results<Ok, ValidationProblem>> PostNew (RegisterDTO registration,  [FromServices] IServiceProvider sp) {

    var userManager = sp.GetRequiredService<UserManager<User>>();

    if (!userManager.SupportsUserEmail)
    {
        throw new NotSupportedException($"User store with email support required.");
    }

    var userStore = sp.GetRequiredService<IUserStore<User>>();
    var emailStore = (IUserEmailStore<User>)userStore;
    var email = registration.Email;

       if (string.IsNullOrEmpty(email) || !_emailAddressAttribute.IsValid(email))
          {
                    return CreateValidationProblem(IdentityResult.Failed(userManager.ErrorDescriber.InvalidEmail(email)));
          }

     var user = new User();
     user.Contact = new Contact
         {
                    FirstName = registration.Firstname,
                    MiddleName = registration.Middlename,
                    LastName = registration.Lastname,
                    PhoneNumbers = new List<PhoneNumber> { new PhoneNumber
                {
                    Number = registration.Phonenumber
                } },
                    Selfie= registration.Profilephoto,
                    IDImage = registration.Idimage

            };

     await userStore.SetUserNameAsync(user, registration.Username, CancellationToken.None);
     await emailStore.SetEmailAsync(user, email, CancellationToken.None);
     var result = await userManager.CreateAsync(user, registration.Password);

       if (!result.Succeeded)
            {
                return CreateValidationProblem(result);
            }

 //   await SendConfirmationEmailAsync(user, userManager, context, email);
      return TypedResults.Ok();
    }

我使用 microsoft identityApiEndpointRouteBuilderExtensions 中的代码编写了此代码,因此它应该可以与其他标识功能完美配合。您可以在 https://github.com/dotnet/aspnetcore/blob/main/src/Identity/Core/src/IdentityApiEndpointRouteBuilderExtensions.cs 上找到实际代码。