5

I am attempting to change asp.net identity's pk system from database nvarchar(128) -> uniqueidentifier and in code from string -> Guid. Following this article based on changing the pk to an int32 I have just one problem I can't seem to get around.

In my Startup.Auth.cs class I have changed the following

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {   //error on the line below
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(TimeSpan.FromMinutes(20), (manager, user) => user.GenerateUserIdentityAsync(manager), (identity) => Guid.Parse(identity.GetUserId()))
            }
        });  

and am getting two errors that I cannot comprehend. The structure of Identity confuses the hell out of me with so many generics. I understand it says it is receiving the wrong parameter type but I have no idea how to remedy the issue.

Errors

Error 1 The best overloaded method match for 'Microsoft.AspNet.Identity.Owin.SecurityStampValidator.OnValidateIdentity(System.TimeSpan, System.Func>, System.Func)' has some invalid arguments

Error 2 Argument 2: cannot convert from 'lambda expression' to 'System.Func>'

Can anyone offer a little insight?

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Adrian
  • 3,332
  • 5
  • 34
  • 52
  • 1
    Maybe it would be helpful for you: http://stackoverflow.com/questions/22504951/aspnet-identity-using-guid-as-key – Moshtaf Jul 31 '14 at 04:04
  • 1
    See IdentityModels, IdentityConfig and Startup.Auth files here for how to implement it: [Samples.PrimaryKeyGuid](https://github.com/suhasj/Samples.PrimaryKeyGuid/tree/master/Samples.PrimaryKeyGuid) – orad May 29 '15 at 07:08

1 Answers1

12
  app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/_layouts/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, WebUser,Guid>(
                    validateInterval: TimeSpan.FromMinutes(30),
                     regenerateIdentityCallback: (manager, user) => 
                    user.GenerateUserIdentityAsync(manager), 
                getUserIdCallback:(id)=>(Guid.Parse(id.GetUserId())))

            }
        });        
Hossam Arafa
  • 136
  • 2
  • 3
  • 3
    That's a lot of code with a description. It is always a good idea to describe *why* the code answers the question. Please [edit] your answer to add a description. – Artjom B. Apr 13 '15 at 15:43
  • If I use this method, I get the exception "UserId not found." after registration. Thrown by GenerateUserIdentityAsync -> manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); Any suggestions? – Erik Nov 21 '16 at 12:43