Custom Provider User Keys

Since release 2.3.0 you have three ways of using provider user keys (main identifiers): user names, integer identities and GUIDs.

Using user names

Primary key in database (or "provider user key" in membership terminology) is the user name string. This is default value. No special configuration and changes in database are needed.

Using integer identities

To use integer identities, set the userKeyType configuration attribute to IntIdentity. In this case, integer identifier is used as primary key. The key is to be generated on database side, by means of the IDENTITY clause or similar mechanism on column named UserId. Is recommended (although not technically required) to specify additional unique index over the UserName table.

The following SQL command can be used to create required table in MS SQL Server:
CREATE TABLE Users (
    UserId                  int identity        NOT NULL,
    UserName                nvarchar(100)       NOT NULL,
    PasswordHash            binary(64)          NOT NULL,
    PasswordSalt            binary(128)         NOT NULL,
    Email                   nvarchar(max)       NOT NULL,
    Comment                 nvarchar(max)       NULL,
    IsApproved              bit                 NOT NULL,
    DateCreated             datetime            NOT NULL,
    DateLastLogin           datetime            NULL,
    DateLastActivity        datetime            NULL,
    DateLastPasswordChange  datetime            NOT NULL,
    CONSTRAINT PK_UsersByIntIdentity PRIMARY KEY CLUSTERED (UserId)
)
CREATE UNIQUE NONCLUSTERED INDEX IX_Users_UserName ON Users (UserName)

Using GUIDs

To use GUID identities, set the userKeyType configuration attribute to Guid. In this case, uniqueidentifier is used as primary key. The key is generated on provideer side, you only need to add uniqueidentifier column named UserId. Is recommended (although not technically required) to specify additional unique index over the UserName table.

In this setting, you can explicitly specify providerUserKey argument in the CreateUser method, as long as it's unique GUID. If you don't specify it, the key would be generated automatically.

The following SQL command can be used to create required table in MS SQL Server:
CREATE TABLE Users (
    UserId                  uniqueidentifier    NOT NULL,
    UserName                nvarchar(100)       NOT NULL,
    PasswordHash            binary(64)          NOT NULL,
    PasswordSalt            binary(128)         NOT NULL,
    Email                   nvarchar(max)       NOT NULL,
    Comment                 nvarchar(max)       NULL,
    IsApproved              bit                 NOT NULL,
    DateCreated             datetime            NOT NULL,
    DateLastLogin           datetime            NULL,
    DateLastActivity        datetime            NULL,
    DateLastPasswordChange  datetime            NOT NULL,
    CONSTRAINT PK_UsersByGuid PRIMARY KEY CLUSTERED (UserId)
)
CREATE UNIQUE NONCLUSTERED INDEX IX_Users_UserName ON Users (UserName)

Sample

See the TableProviderSampleUserKeys sample for info how to use and configure this feature.

Last edited Jan 27, 2011 at 1:06 AM by altair, version 2

Comments

No comments yet.