I want to log users automatically if they click a link in an email, e.g. example.com/action?_ul=blah-guid. It would detect the '_ul' and automatically login the user. The url can be one of many different ones, so I want to detect if the query string param is present and automatically log them in using this special key if it is.
I've previously done this with the old identity provider in Global.asax.cs using Application_BeginRequest, e.g.
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request["_ul"] != null)
{
var userService = new UserService();
userService.QuickLogin(Request["_ul"]);
}
}
However, in this project we're using Asp.Net Identity and it seems the OwinStartup is happening after this point and is throwing an error:
No owin.Environment item was found in the context.
This happens in the initialisation of the service:
public UserService()
{
signInManager = HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>();
}
I tried using an ActionFilter but this happens after authentication.
I'm unsure how to do this in Asp.Net Identity, I want to keep normal forms authentication, but also detect if there is a particular query string value, log out if they're presently logged in, then log in using the special key.
What's the correct way to do this additional login check?