Nannette Thacker ShiningStar.net

ASP.net Web Application Development

Sponsors

News

See all Blog Posts by Nannette.

Nannette Thacker, consultant and owner of Shining Star Services LLC, specializes in development of custom dynamic database driven web applications utilizing ASP.net technologies. Nannette has been developing ASP sites since 1997. Nannette has written numerous articles on web development techniques and tutorials.

Nannette is the owner and developer of ChristianSinglesDating.com.

 Subscribe in a reader





View Nannette  Thacker's profile on LinkedIn

CreateUser Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ

From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the CreateUser method. I welcome any suggestions for improvement.
public override MembershipUser CreateUser(string username, string password,
    string email, string passwordQuestion, string passwordAnswer,
    bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
    // only first 3 fields are passed in from the AccountModels.cs
    try
    {
        status = UsernameExists(username);
        if (status == MembershipCreateStatus.DuplicateUserName)
        {
            return null;
        }
        status = DuplicateEmail(email);
        if (status == MembershipCreateStatus.DuplicateEmail)
        {
            return null;
        }

        byte[] hashedPassword = HashPassword(password.Trim());
        GetValues gv = new GetValues();
        string ipAddress = gv.getIPAddress();
        int userStatus = 
            Convert.ToInt32(SSS.GlobalListValues.Enums.UserStatusCode.Active);

        using (var context = new SSSEntities())
        {
            UserProfile newUser = new UserProfile()
            {
                Email = email,
                UserPassword = hashedPassword,
                UserName = username,
                DateCreated = DateTime.Now,
                DateUpdated = DateTime.Now,
                DatePasswordLastChanged = DateTime.Now,
                DateLastLogin = DateTime.Now,
                UserStatusCode = userStatus,
                IpAddress = ipAddress,
            };


            // insert the User Role
            int userRole = 
                Convert.ToInt32(SSS.GlobalListValues.Enums.UserRoleCode.User_Public);
            // look up the desired user role : 
            // uses a UserRole join table with a many to many relation
            // between the UserProfile table and the ListValue table
            ListValue ur = context.ListValues
                .SingleOrDefault(lv => lv.ListValueId == userRole);
            newUser.UserProfileUserRoles.Add(ur);

            context.UserProfiles.AddObject(newUser);
            context.SaveChanges();

            // NKT: after creation, go back and retrieve the auto-generated identity key and 
            // update the userId's for the created and updated userId
            int userId = newUser.UserId;
            newUser.CreatedUserId = userId;
            newUser.UpdatedUserId = userId;

            context.SaveChanges();
            status = MembershipCreateStatus.Success;
            return GetMembershipUser(newUser);
        }
    }
    catch (ArgumentException)
    {
        status = MembershipCreateStatus.ProviderError;
        return null;
    }

}

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)