Posts

Showing posts from January, 2016

Passing the current ClaimsIdentity to the regenerateIdentity callback

I wanted to persist some extra claims that weren't driven directly from the database. That was all well and good until the user identity was refreshed and the extra claims were lost. The template code for Startup.cs looks like this: 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<ApplicationUserManager, ApplicationUser>( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) } }); It tur...

A coder's new year's resolution

When I write and review code I'm always looking to have one thing happen per line. I've mostly favoured this because of how much more readable it makes the code e.g. see this review http://codereview.stackexchange.com/a/111706/22816. However, I refactored some code and introduced a massive bug (caught by tests!) because I missed a side effect... The real code was a lot longer (100+ LoC method - hence the refactor). It was something of this form though: var i = 0; var maxAttempts = 5; while ( true ) { var workRequest = someQueue.GetWork(); var workResult = someService.Execute(workRequest); if (!workResult.IsSuccess) { Log( "{0}, {1}, {2}" , workRequest.Name, i++, workResult.ErrorsDisplay); } if (i > maxAttempts) { throw new DomainException( "Some details!" ); } } When I refactored the code I changed the line that incremented the  i  variable and lost the side effect! It's a really...