Trying to read FormsAuthentication tickets to read in other areas of site
- by Pasha Aryana
Hi,
NOTE: I have included 3 links in here to my localhost areas but could not submit the post so I seperetaed them with a space character so it would post on stackoverflow.
I currently have 2 ASP.NET MVC apps in my solution. First I run the first one by setting it to be startup project. It goes to the login page, from there once the data has been entered I execute the following code:
var authTicket = new FormsAuthenticationTicket(1, login.LoginDataContract.MSISDN, DateTime.Now,
DateTime.Now.AddMinutes(Convert.ToDouble("30")), true, "");
string cookieContents = FormsAuthentication.Encrypt(authTicket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieContents)
{
Expires = authTicket.Expiration,
//Path = FormsAuthentication.FormsCookiePath
//Path = "http://localhost"
Domain = ""
};
if (System.Web.HttpContext.Current != null)
{
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
As you can see I have set the Domain = "", so theoretically speaking it should work on any thing under my http: //localhost. Then I have set the persist security of the cookie to true so I can access it from any where under localhost.
The cookie writes fine and I get logged in and all godd for now. BTW the url for this login page is: http //localhost/MyAccount/Login
Now then I stop the solution and set the other MVC apps to be the startup. Then I run it. The URL for the second site is: http: //localhost/WebActivations/
Here is the code in the other apps start controller:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
// PASHA: Added code to read the authorization cookie set at
// login in MyAccount *.sln
for (int i = 0; i < System.Web.HttpContext.Current.Request.Cookies.Count;i++)
{
Response.Write(System.Web.HttpContext.Current.Request.Cookies[i].Name + " " + System.Web.HttpContext.Current.Request.Cookies[i].Value);
}
HttpCookie authorizationCookie = System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName.ToString()];
// decrypt.
FormsAuthenticationTicket authorizationForm = FormsAuthentication.Decrypt(authorizationCookie.Value);
ViewData["Message"] = authorizationForm.UserData[0].ToString();
return View();
}
public ActionResult About()
{
return View();
}
The problem is in this Home controller when I run the solution it cannot read the authentication cookie, you see the loop there it does not find the .ASPXAUTH cookie.
But once it crashes in Firefox I have a look in the Page Info and then security and Cookies and its there and its the same cookie.
What am I doing wrong?