In my ASP.NET 4.0 application, I am suppposed to be able to create user accounts through the website. These accounts should tie into the aspnet Membership and Roles. Upon creating the membership, the aspnet UserId does not get entered into the database.
I've been stuck on this same problem for going on 3 days now and the person that originally wrote this code can't figure out why this won't work for me. If anyone can help me get a UserId to pass, I would be extremely grateful!
When I look at my page, it will say the user was added successfully, but when I try to edit the new user, in debug mode, when hovering over each line on the page, I see that an aspnet UserID and Role have never been added with the new record. How is this able to add a record with a null id and member role, yet add everything else just fine?
In order to check if that user is actually being created successfully use the Membership.CreateUser overload which accepts a CreateUserStatus enum and then check the status of that to ensure your user is actually being created.
I would use the above just to make sure all is well and then try to get the userid by doing something like:-
var userid = (Guid)Membership.GetUser(username).ProviderUserKey;
If this fixed your issue then please 'Mark as Answer'
I tried that, but I get an error when trying to user public override Membership CreateUser() saying 'System.Web.Security.Membership': static types cannot be used as return types. I changed it to public static Membership CreateUser() but I get the same error.
grinnellja
Participant
1221 Points
624 Posts
membership not being created
Feb 05, 2013 10:10 PM|LINK
In my ASP.NET 4.0 application, I am suppposed to be able to create user accounts through the website. These accounts should tie into the aspnet Membership and Roles. Upon creating the membership, the aspnet UserId does not get entered into the database. I've been stuck on this same problem for going on 3 days now and the person that originally wrote this code can't figure out why this won't work for me. If anyone can help me get a UserId to pass, I would be extremely grateful!
public Users CreateAccount(string fullName, string userName, string emailAddress, string roleName) { MembershipUser mUser = Membership.CreateUser("jamietest6@testing.com", "newpassword1234", "jamietest6@testing.com"); Membership.UpdateUser(mUser); Guid uID = (Guid)mUser.ProviderUserKey; Roles.AddUserToRole("jamietest6@testing.com", "Members"); //if (mUser == null) //{ //return null; //} // Create user's account Users user = new Users(); user.FullName = "jamietest6@testing.com"; user.Password = "newpassword1234"; user.EmailAddress = "jamietest6@testing.com"; user.aspnet_Users = new aspnet_Users().Load<aspnet_Users>(uID); user.SaveOrUpdate(); }In debug mode, it will stop at
and show that the
has a value of
and the
has a value of
When I look at my page, it will say the user was added successfully, but when I try to edit the new user, in debug mode, when hovering over each line on the page, I see that an aspnet UserID and Role have never been added with the new record. How is this able to add a record with a null id and member role, yet add everything else just fine?
FROM THE DATABASE:
aspnet_Membership table
Members.Users table
ammd
Participant
1349 Points
257 Posts
Re: membership not being created
Feb 05, 2013 10:55 PM|LINK
In order to check if that user is actually being created successfully use the Membership.CreateUser overload which accepts a CreateUserStatus enum and then check the status of that to ensure your user is actually being created.
I would use the above just to make sure all is well and then try to get the userid by doing something like:-
var userid = (Guid)Membership.GetUser(username).ProviderUserKey;
grinnellja
Participant
1221 Points
624 Posts
Re: membership not being created
Feb 06, 2013 02:52 PM|LINK
I tried that, but I get an error when trying to user public override Membership CreateUser() saying 'System.Web.Security.Membership': static types cannot be used as return types. I changed it to public static Membership CreateUser() but I get the same error.
ammd
Participant
1349 Points
257 Posts
Re: membership not being created
Feb 06, 2013 03:12 PM|LINK
Do you get the error on createuser, or getuser?
Declare as follows:-
CreateUserStatus _status;
and pass in like CreateUser(uname,password,,,,out _status);