Custom Membership with Microsoft.AspNet.Identity - CreateLocalUser fails

asp.net-identity asp.net-mvc asp.net-mvc-5 entity-framework owin

Question

Using Visual Studio 2013, I've been attempting to develop a customized version of the new Identity features in ASP.NET 4.5 (Microsoft.AspNet.Identity). I've simplified my code after spending countless hours tinkering with it in an effort to get it to work faultlessly. My code is listed below. The database tables are generated when performing a local registration, but the CreateLocalUser function does not succeed. I'm hoping someone can point out the adjustments that are required.

Models/MembershipModel.cs

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace thePulse.web.Models
{
    public class PulseUser : IUser
    {
        public PulseUser() { }
        public PulseUser(string userName) 
        {
            UserName = userName;
        }

        [Key]
        public string Id { get; set; }
        [Required]
        [StringLength(20)]
        public string UserName { get; set; }
        [StringLength(100)]
        public string Email { get; set; }
        [Column(TypeName = "Date")]
        public DateTime? BirthDate { get; set; }
        [StringLength(1)]
        public string Gender { get; set; }
    }

    public class PulseUserClaim : IUserClaim
    {
        public PulseUserClaim() { }

        [Key]
        public string Key { get; set; }
        public string UserId { get; set; }
        public string ClaimType { get; set; }
        public string ClaimValue { get; set; }

    }

    public class PulseUserSecret : IUserSecret
    {
        public PulseUserSecret() { }
        public PulseUserSecret(string userName, string secret)
        {
            UserName = userName;
            Secret = secret;
        }

        [Key]
        public string UserName { get; set; }
        public string Secret { get; set; }

    }

    public class PulseUserLogin : IUserLogin
    {
        public PulseUserLogin() { }
        public PulseUserLogin(string userId, string loginProvider, string providerKey) 
        {
            LoginProvider = LoginProvider;
            ProviderKey = providerKey;
            UserId = userId;
        }

        [Key, Column(Order = 0)]
        public string LoginProvider { get; set; }
        [Key, Column(Order = 1)]
        public string ProviderKey { get; set; }
        public string UserId { get; set; }
    }

    public class PulseRole : IRole
    {
        public PulseRole() { }
        public PulseRole(string roleId)
        {
            Id = roleId;
        }

        [Key]
        public string Id { get; set; }
    }

    public class PulseUserRole : IUserRole
    {
        public PulseUserRole() { }

        [Key, Column(Order = 0)]
        public string RoleId { get; set; }
        [Key, Column(Order = 1)]
        public string UserId { get; set; }
    }

    public class PulseUserContext : IdentityStoreContext
    {
        public PulseUserContext(DbContext db) : base(db)
        {
            Users = new UserStore<PulseUser>(db);
            Logins = new UserLoginStore<PulseUserLogin>(db);
            Roles = new RoleStore<PulseRole, PulseUserRole>(db);
            Secrets = new UserSecretStore<PulseUserSecret>(db);
            UserClaims = new UserClaimStore<PulseUserClaim>(db);
        }
    }

    public class PulseDbContext : IdentityDbContext<PulseUser, PulseUserClaim, PulseUserSecret, PulseUserLogin, PulseRole, PulseUserRole>
    {
    }
}

Controllers/AccountController.cs has been modified

public AccountController() 
{

    IdentityStore = new IdentityStoreManager(new PulseUserContext(new PulseDbContext()));
    AuthenticationManager = new IdentityAuthenticationManager(IdentityStore);
}

//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        try
        {
            // Create a profile, password, and link the local login before signing in the user
            PulseUser user = new PulseUser(model.UserName);
            if (await IdentityStore.CreateLocalUser(user, model.Password))
            {
                await AuthenticationManager.SignIn(HttpContext, user.Id, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Failed to register user name: " + model.UserName);
            }
        }
        catch (IdentityException e)
        {
            ModelState.AddModelError("", e.Message);
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

The CreateLocalUser method failing causes this implementation to fail, as I previously stated (Microsoft.AspNet.Identity.EntityFramework). I'm baffled as to why.

1
22
7/29/2013 9:45:39 AM

Accepted Answer

ZZZ_tmp
12
7/29/2013 6:47:51 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow