12

I am using asp.net identity to create new user but getting error:

Cannot insert the value NULL into column 'Id', table 'Mydb.dbo.AspNetUsers'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated

But here I don't have any such table like AspNetUsers but instead I have my own table that is Users.

Code: Web.config: 2 conection strings

 <add name="myEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=;initial catalog=mydb;user id=sa;password=sdfsdfsdf;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
 <add name="MyConnString" connectionString="data source=;initial catalog=Mydb;user id=sa;password=sdfsdfsdf;" providerName="System.Data.SqlClient" />

IdentityModel.cs:

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            this.SecurityStamp = Guid.NewGuid().ToString();
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public virtual string Email { get; set; }
        public string Password { get; set; }
        public string Role { get; set; }
        public Nullable<bool> IsActive { get; set; }
        public Nullable<int> CreatedBy { get; set; }
        public Nullable<System.DateTime> CreatedDate { get; set; }
        public Nullable<System.DateTime> LastLogin { get; set; }

        public ApplicationUser()
        {

        }

        public ApplicationUser(string email, string firstName, string lastName, string designation, bool isActive)
        {
            Email = email;
            FirstName = firstName;
            LastName = lastName;
            Designation = designation;
            IsActive = isActive;
        }
    }
  public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("MyConnString", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

UserStore1.cs:

public class UserStore1 : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>
    {
        private readonly HttpContext _httpContext;
        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>(new ApplicationDbContext());

        public System.Threading.Tasks.Task CreateAsync(ApplicationUser user)
        {
            HttpContext.Current = _httpContext ?? HttpContext.Current;
            var context = userStore.Context as ApplicationDbContext;
           context.Users.Add(user);
           context.Configuration.ValidateOnSaveEnabled = false;
           context.SaveChanges();
            return Task.FromResult(true);
        }
     }

Controller:

     [Authorize]
    public class AccountController : Controller
    {
        public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore1()))
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }
        public UserManager<ApplicationUser> UserManager { get; private set; }
[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(string email, string password, bool rememberMe = false, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName= "Abc",
                    LastName= "Pqr",
                    UserName="[email protected]",
                    SecurityStamp = Guid.NewGuid().ToString()
                };

                var result= await UserManager.CreateAsync(user,"123456");
            }
            return View();
        }
    }

Note: I have autogenerated Id in my database table field and that Id is Int.

Update: I am using database first(edmx) and the table that I am using are custom tables for inserting new records(for eg:Users).

At first I have implemented microsoft asp.net identity as shown in below question but 1 user pointed out that I am not using ApplicationUser class which is responsible for handling sign in,cookies etc so I am now trying to use ApplicationUser class:

How to give custom implementation of UpdateAsync method of asp.net identity?

I am really now regretting over my decision to choose Microsoft Identity Framework for Authentication purpose as because I am really finding it complex but now as I have move forward I have to go with it.

7
  • It seems to me that you're missing something. You haven't defined your model in OnModelCreating for your ApplicationDbContext. Check this answer Commented Sep 4, 2016 at 16:06
  • @LeftyX sorry but I am not using code first.i am using databse first(.edmx) and I have already seen some of your answers and infact tried it but unfortunately you have used code first and I am using edmx Commented Sep 4, 2016 at 17:55
  • 1
    I don't have much time these days but I'll have a look into this over the weekend. If you could isolate the code and share it somewhere it would be helpful. Cheers. Commented Sep 8, 2016 at 7:39
  • @LeftyX:Yeah sure.if you want i can send you my project which contains only microsoft identity implementation and database script so you can take a look.can i send you link for both of that to download or what you want ?? Commented Sep 8, 2016 at 7:49
  • 1
    Please do. Publish it somewhere so that I can download it. Cheers. Commented Sep 8, 2016 at 8:23

1 Answer 1

7
+25

The inconsistency i found, in ApplicationUser class you are declaring property Idand Email which is wrong because the IdentityUser class already have those properties. This may arise the issue. But you can override them if necessary. Also the constructor you are using isn't necessary. The ApplicationUser class should be:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        this.SecurityStamp = Guid.NewGuid().ToString();
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }    
    public string Password { get; set; }
    public string Role { get; set; }
    public bool? IsActive { get; set; }
    public int? CreatedBy { get; set; }
    public DateTime? CreatedDate { get; set; }
    public DateTime? LastLogin { get; set; }

}

Second thing, you are creating the user inside Login action which is also not valid. you should do it inside Register action. Following in an example:

 public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
          var user = new ApplicationUser
            {
                FirstName = "Abc",
                LastName = "Pqr",
                UserName = "[email protected]",
                Email= model.Email,
                Password= model.Password,
                PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password),
                SecurityStamp = Guid.NewGuid().ToString()
            };

            var result = await UserManager.CreateAsync(user);
        if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);                    
                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        return View(model);
    }

Hope this will help :)

Sign up to request clarification or add additional context in comments.

9 Comments

This is not the problem that i am creating user in login or in register method.this was just for testing that custom implementation of CreateAsync method is working fine or not.If you see my UserStore1 class you will notice custom implementation of CreateAsync method
you are not using the custom implementation of CreateAsync. look at the signature of method. you are using the default CreateAsync of UserManager.
When I put a debugger on my custom createasync method which is on userstore1 then debugger hit my method
I actually focused on ApplicationUser class. Did the change resolve the issue or any other error message ?
Nope still getting same error and I guess that is not a problem because what I think is that problem is related to entity framework and applicationdbcontext
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.