Background
I am developing iterative migration from a legacy ASP.NET forms application to an MVC 4 application.
This migration occurs in stages, one section at a time, so you need to run both the legacy and the new MVC application at the same time. For some sections, the user accesses the new application, and for sections that have yet to be migrated, the user returns (or stays on) to the outdated application.
On the server, the applications are structured as follows:
LEGACY (.NET 2.0 forms app)
|
|
Thus, the foodefault page call is - /foo.aspx, and on V2 - /V2/foo.
Everything is in C #, but note that the legacy application runs on .NET 2.0, and the V2 application runs on .NET 4.5 . I'm not sure if that matters, but I thought it would be important to note.
Problem
I cannot successfully share user / authentication status between applications. Obviously, if a user has registered in an outdated application, I need to grab his cookie (or take similar actions) in V2 when the V2 page is called so that the user does not receive an invitation to log in again.
What i tried
I found the same elements authenticationand machinekeyin both files web.config:
<authentication mode="Forms">
<forms cookieless="UseCookies" defaultUrl="~/someUrl" loginUrl="/" path="/" protection="All" timeout="240" enableCrossAppRedirects ="true" domain="someDomain.com" requireSSL="false" />
</authentication>
<machineKey validationKey="DE78CF63226. . .788658D142AB881" decryptionKey="0B97E8BA4C4EB4B4C524. . .54E4622E14168D2D5C84461FA2" validation="SHA1" decryption="AES" />
I tried this code in the Global.asax application of V2 application:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (authCookie == null)
{
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
{
return;
}
if (authTicket == null)
{
return;
}
string[] roles = authTicket.UserData.Split(new char[] { '|' });
var id = new FormsIdentity(authTicket);
var principal = new GenericPrincipal(id, roles);
Context.User = principal;
}
, , , V2, , V2, Legacy V2.
HomeController , - :
ViewBag.UserName = User.Identity.Name;
, ViewBag.UserName html , .