Deploying OWIN using existing database name validation?

I want to implement OWIN according to an example that I can find here: http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

However, since this method of work is new to me, especially using my own database, I would like to get some recommendations. I can send my registration request without any problems.

The message sends me to 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; } } 

This calls the following code:

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

ApplicationUserManager:

 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; } } 

But for some strange reason, I get

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

Even if I do not use the name anywhere! Where does this name come from?

Therefore, I assume that I am doing something wrong, but it is difficult to debug without a clear explanation of how to implement OWIN with an existing database.

Below my context / entity table and users, which I would like to use to store my user data.

context:

 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; } } 

Users: IdentityUser:

 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; } } 

Edit:

If I add UserName back to the new users object before trying to call CreateAsync, the error will disappear, but instead I get another:

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

Edit II:

I also have this problem in the training code! Is this just a bug in .NET?

Change III

I tried to do an override, as you can see in the partial Users class above. But I still have the same error.

+5
source share
4 answers

I finally got a job.

In the end, I had to add an adjustment to the partial Users class (which is the table in my SQL database), so that UserName and Password override the IDentityUser fields (so it's not like in my question):

 public partial class users { public int Id { get; set; } public string UserName {get; set; } public string Password { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime? CreatedDate { get; set; } public DateTime? ModifiedDate { get; set; } } 

Then I just had to pass the password and username when creating the user:

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

The problem is that I did not add UserName to my new user before I call UserManager.CreateAsync. If I add UserName, I will have the same problem: MVC 5 transactions IdentityDbContext and DbContext

This issue seems to indicate that ASP.NET Identity uses the UserName property in the request, but the Entity Framework claims that the property does not map to the database column in your Entity Model (EDML).

0
source

I do not see a lot of problems with your code

  • Remove DbSet users from DaumAuctionEntities class (see this )
  • Remove Id property from user class
  • Set CreatedDate property when creating new users inside the register Method

Then you should be fine

In addition, about:

No, actually it conveys. I think it’s a manager, but when I try to debug this code, the debugger states that it jumped over it (most likely due to the fact that it is an external library from Microsoft).

If you end the call with try / catch, you may see an error message

0
source

I got a few problems with the code you shared here, but not quite the way you mentioned. I shared an example on git for your reference. Try it for yourself and let me know if you see any problems.

Git URL - https://github.com/JitJDN/OWINStack

Look, the code is the same as yours:

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(); 
0
source

Source: https://habr.com/ru/post/1243455/


All Articles