locked
Snafu missing TKey on UseCookieAuthentication extension method RRS feed

  • Question

  • User-217557848 posted

    Hello,

    I am specifying to UseCookieAuthentication with the following code:

                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/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<SimulatorUserManager, User, Guid>(
                            validateInterval: TimeSpan.FromMinutes(30),
                            regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
                            // TODO: look up the IUser from the ClaimsIdentity
                            getUserIdCallback: claimsIdentity => Guid.Empty
                            )
                    }
                });            
    

    Wait for it...

    I specify my UserManager, User, and the TKey; in this case, Guid.

    In this particular version, figuring this was the best fit for my scenario, "userId" is the User.Id in the domain model.

    However, I am pretty sure this one has overlooked that detail in the getUserIdCallback, in that I only have a claimsIdentity. Which I could return claimsIdentity.Name, if I thought that was my Id, which it is not.

    The defaults are baked in using the System.String as the key. Could that be one reason why?

    I want to use the Guid instead of String, but this detail seems to have been overlooked.

    Suggestions?

    Thank you...

    Regards,

    Michael Powell

    Sunday, February 28, 2016 7:47 PM

Answers

  • User-217557848 posted

    Figured it out. Loosely based on the SO blog.

                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/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<SimulatorUserManager, User, Guid>(
                            validateInterval: TimeSpan.FromMinutes(30),
                            regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
                            // TODO: look up the IUser from the ClaimsIdentity
                            getUserIdCallback: claimsIdentity =>
                            {
                                var id = claimsIdentity.GetUserId<string>();
                                return Guid.Parse(id);
                            })
                    }
                });
    

    In a somewhat roundabout way, the id is stored as a string, to be expected. GetUserId<string>() fetches this from the claimsIdentity. Then parse as a Guid.

    Thank you...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 28, 2016 7:57 PM