How to use OAuth with Forms auth
See the question and my original answer on StackOverflowForms auth usually puts a cookie to the authenticated user, so what you can do is do the same, and send the exact same cookie that forms auth would have sent.
So, on sucessfull OAuth callback from the provider, you can just do something like this:
// here, we are called on a successful OAuth auth, so
// let's do Forms login by ourselves
HttpCookie authCookie = GetFormsAuthCookie(email, true, true);
context.Response.Cookies.Add(authCookie);
With the GetFormsAuthCookie method defined like this:
private static HttpCookie GetAuthCookie(string userName, bool createPersistentCookie, bool httpOnly)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, userName, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout), createPersistentCookie, "SocialEmailLogin", FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
if (encryptedTicket == null)
throw new Exception("Obviously, something went wrong here. That shouldn't happen.");
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = httpOnly;
cookie.Path = FormsAuthentication.FormsCookiePath;
if (FormsAuthentication.RequireSSL)
{
cookie.Secure = true;
}
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
}
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
return cookie;
}