2

I'm overriding asp.net membership provider with my custom membership provider. I would like to know what kind of type do I have to set for my ProviderUserKey in my User model?

I tried this:

public class User
{
    [Key]
    public ????? ProviderUserKey { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public string PasswordQuestion { get; set; }
    ...
}

I don't know how to generate a new ProviderUserKey value when creating a new user. Any ideas?

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Bronzato
  • 9,438
  • 29
  • 120
  • 212

1 Answers1

2

According to the virtual property, it should be

public Object ProviderUserKey { get; set; }

For your first question:

I would like to know what kind of type do I have to set for my ProviderUserKey in my User model?

From the MSDN page on the MemershipUser class:

The type of the identifier depends on the MembershipProvider or the MembershipUser. In the case of the SqlMembershipProvider, the ProviderUserKey can be cast as a Guid, since the SqlMembershipProvider stores the user identifier as a UniqueIdentifier.

So, it depends on how you're storing the user identifier (as an int / guid / varchar, etc). You type it as Object, but cast it to the proper data type based on your implementation.

And your second question:

I don't know how to generate a new ProviderUserKey value when creating a new user. Any idea?

When you create the new user, and they are added to whatever you're storing them in (relational database, XML file, etc), retrieve the unique identifier generated by that process. If you are not generating a unique user identifier at that point...you need to be =)

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • I didn't understand how can I generate my own Guid for this ProviderUserKey can you please elaborate? – Moran Monovich Apr 08 '12 at 08:11
  • @MoranMonovich It would be something like `System.Guid g = System.Guid.NewGuid();`. More info can be found in [this Stack Overflow post](http://stackoverflow.com/questions/2344098/c-sharp-how-to-create-a-guid-value) and [the documentation](http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx). – Josh Darnell Apr 08 '12 at 21:49
  • Thanks, I used the methods CreateUser() can accept three parameters and is creating the Guid automatically. – Moran Monovich Apr 09 '12 at 04:41