How to create a custom login page in EPiServer MVC with OWIN

 

It's very easy to create a custom login page using the new AspNetIdentityPackage however the biggest problem I encountered was the lack of support for RedirectUrl, user would always be redirected back to the start page.

However, after a bit of digging I made it to work. Please take a look: Custom Login Page

The diff to the current Alloy MVC is here: Custom Login Page Diff

You can just clone my repository and follow the Readme to see it in action.

The problem was to override our default behavior of handling ReturnUrl which we always read from QueryString, however in case of a custom login screen that value is passed in form data. It was easy to do by overriding the default UISignInManager with a custom implementation with an overridden SignIn method that would skip the redirect and let your login controller to do the work.

public class CustomApplicationUISignInManager<T> : ApplicationUISignInManager<T>
  where T : IdentityUser, IUIUser, new()
{
  private readonly ServiceAccessor<ApplicationSignInManager<T>> _signInManager;

  public CustomApplicationUISignInManager(ServiceAccessor<ApplicationSignInManager<T>> signInManager)
    : base(signInManager)
  {
    _signInManager = signInManager;
  }

  public override bool SignIn(string providerName, string userName, string password)
  {
    return _signInManager().SignIn(userName, password, string.Empty);
  }
}

and registration in Startup.cs

// Add CMS integration for ASP.NET Identity
app.AddCmsAspNetIdentity<ApplicationUser>();
app.CreatePerOwinContext<UISignInManager>((options, context) =>
    new CustomApplicationUISignInManager<ApplicationUser>(context
        .Get<ApplicationSignInManager<ApplicationUser>>));