Usman ur Rehman Ahmed's blog

Cannot unbox 'currentUser.ProviderUserKey' as a 'System.Guid' System.Guid

Today I was testing MySql membership provider using MySql data connector 6.2.4. While the setup for running form based authentication on top of MySql membership was fairly straight forward, something that surprised me for a bit was to get an unexpected exception when I tried to read the ProviderUserKey,

    MembershipUser currentUser = Membership.GetUser();
    Guid currentUserId = (Guid)currentUser.ProviderUserKey;

at second line I got the exception, "Cannot unbox 'currentUser.ProviderUserKey' as a 'System.Guid' System.Guid". Why would that be I thought when ProviderUserKey is object type?

Looking closely I identified that MySQLMembershipProvider offers Integer type ProviderUserKey as shown below,

Mysqlmembershipprovider
Why is that? Perhaps becuase MySql doesn't offer an MS SQL server's UniqueIdentifer equivelent data type intrinsically out of box and unqie identification is achieved by function UUID() as given here,

http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_...

11
To Posterous, Love Metalab