gpt4 book ai didi

c# - 使用现有的数据库名称验证实现 OWIN?

转载 作者:太空狗 更新时间:2023-10-29 23:50:05 40 4
gpt4 key购买 nike

我希望按照我可以在此处找到的示例实现 OWIN: http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

但是,由于这种工作方式对我来说是新的,尤其是使用我自己创建的数据库时,我需要一些指导。我可以毫无问题地提交我的注册请求。

该帖子将我带到 AccountController:

    [AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

try {
var email = model.Email;
var password = model.Password;

var user = new users() {
Email = email,
PasswordHash = password,
Password = password
};

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

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

return Ok();
}
catch(Exception ex)
{
throw;
}
}

这会触发以下代码:

    public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}

应用程序用户管理器:

public class ApplicationUserManager : UserManager<users>
{
public ApplicationUserManager(IUserStore<users> store)
: base(store)
{
}

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<users>(context.Get<DaumAuctionEntities>()));
var dataProtectionProvider = options.DataProtectionProvider;

// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = true,
};

if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<users>(dataProtectionProvider.Create("ASP.NET Identity"));
}

return manager;
}
}

但是由于一些奇怪的原因我得到了

 modelState: {undefined: ["Name cannot be null or empty."]}

即使我不在任何地方使用名字?!这个名字从何而来?

所以我认为我做错了什么,但如果没有关于如何使用现有数据库实现 OWIN 的明确解释,很难调试。

在我想用来存储用户数据的上下文/实体和用户表下方。

上下文:

public partial class DaumAuctionEntities : IdentityDbContext<users>
{
public DaumAuctionEntities()
: base("name=DaumAuctionEntities")
{
}

public DbSet<addresses> addresses { get; set; }
public DbSet<auctions> auctions { get; set; }
public DbSet<images> images { get; set; }
public DbSet<users> users { get; set; }
}

用户:身份用户:

public partial class users : IdentityUser
{
public override string UserName
{
get
{
return Email;
}
set
{
Email = value;
}
}

override public string PasswordHash
{
get
{
return Password;
}

set
{
Password = value;
}
}

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<users> manager, string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}

public partial class users
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public System.DateTime CreatedDate { get; set; }
public Nullable<System.DateTime> ModifiedDate { get; set; }

}

编辑:

如果我在尝试调用 CreateAsync 之前将 UserName 添加回我的新用户对象,则错误消失但我得到另一个错误:

"The specified type member 'UserName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."

编辑二:

我在教程代码中也有这个问题!这只是 .NET 中的一个可怕的错误?

编辑三

如您在上面的部分类 Users 中所见,我尝试进行覆盖。但是我仍然有同样的错误。

最佳答案

The issue is due to the fact that I did not add UserName to my new user before I call UserManager.CreateAsync. If I add the UserName I have the exact same issue as this: MVC 5 IdentityDbContext and DbContext transactions

该问题似乎表明 ASP.NET Identity 在查询中使用了 UserName 属性,但 Entity Framework 认为该属性未映射到实体模型中的数据库列(EDML)。

关于c# - 使用现有的数据库名称验证实现 OWIN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522455/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com