无法将自定义应用程序用户字段保存到具有 asp.net 身份和 MySql 提供程序的数据库中



在我的案例中,我在azure云上有现有的mvc项目。有必要添加一个web api来识别外部用户,还决定使用Mysql来存储有关用户的信息。我使用微软的以下说明配置了web api:https://learn.microsoft.com/en-us/aspnet/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider在设置了提供者之后,我使用默认的asp.net web api应用程序来设置其余的功能。以下是我的配置:

IdentityModels.cs

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using AspNet.Identity.MySQL;
using **.Web.App_Start;
namespace **.Web.Models
{

public class CustomerPortalUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<CustomerPortalUser> manager, string authenticationType)
{
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
return userIdentity;
}
public bool IsActive { get; set; }
public string IdNumber { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string PostalCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public string City { get; set; }
}
public class PortalUserRole : IdentityRole
{
public PortalUserRole() : base() { }
public PortalUserRole(string name) : base(name) { }
}
public class PortalUserDbContext : MySQLDatabase
{
public PortalUserDbContext(string connectionName)
: base(connectionName)
{
}
public static PortalUserDbContext Create()
{
return new PortalUserDbContext("PortalAuth");
}
}
}

AccountController.cs注册方法:

public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
BaseResponse response = new BaseResponse();
response.IsSuccess = false;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = new CustomerPortalUser() { 
UserName = model.Email, 
Email = model.Email,
Address1 = model.Address1,
Address2 = model.Address2,
Address3 = model.Address3,
PostalCode = model.PostalCode,
City = model.City,
IdNumber = model.IdNumber,
LastName = model.LastName,
FirstName = model.FirstName
};
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
response.IsSuccess = true;
return Ok();
}

IdentityConfig.cs

public class CustomerPortalUserManager : UserManager<CustomerPortalUser>
{
public CustomerPortalUserManager(IUserStore<CustomerPortalUser> store)
: base(store)
{
}
public static CustomerPortalUserManager Create(IdentityFactoryOptions<CustomerPortalUserManager> options, IOwinContext context)
{
var manager = new CustomerPortalUserManager(new UserStore<CustomerPortalUser>(context.Get<PortalUserDbContext>()));
manager.UserValidator = new UserValidator<CustomerPortalUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<CustomerPortalUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}

以及我的启动。Auth.cs

public void ConfigurePortalAuth(IAppBuilder app)
{
app.CreatePerOwinContext(PortalUserDbContext.Create);
app.CreatePerOwinContext<CustomerPortalUserManager>(CustomerPortalUserManager.Create);

app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/portalApi/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/portalApi/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
}

但当我试图用我的自定义字段注册新用户时

public string IdNumber { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string PostalCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public string City { get; set; }

用户将被创建为具有默认标识值,例如我在请求中传递的密码和用户名。但是我的所有自定义值都将等于NULL,而不是请求中传递的值。此外,如果我会得到这样的用户信息:

CustomerPortalUser user = await userManager.FindAsync(context.UserName, context.Password);

即使自定义值在数据库中不为null,它们在上面的结果中也将为null。使用的包装:

AspNet.Identity.MySQL 1.0.0.38741
MySql.Data 6.10.9
MySql.Data.Entity 6.10.9

为什么不尝试使用IdentityDbContext作为DbContext的基类,在那里可以传递IdenityUserIdentityRole模型。

public class PortalUserDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>

我不熟悉这个MySQLDatabase类及其来源,但它似乎不接受您的自定义模型。至少没有显示为片段。

我正在使用ASP。Net Identity和EF以及MySQL,作为一个适配器,我对Pomelo.EntityFrameworkCore.MySqlNuGet包很满意——如果你对SQL级别的数据有任何问题,你可以选择这个。

最新更新