i have two methodes : one for registering new user AddNewUser(MemberRegisterModel mm) , and another for creating cookie for him CreateCookie(MemberLoginModel member).
i want to use this cookie that has been created after registering to show username top of all page until he log off.
i traced my code and saw cookie was created.
i use this code in HeaderPartial.cshtml to give username from cookie.
<div id="top">
@if (HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName] != null)
{
HttpCookie cookie =
HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
var formAuthTicket = FormsAuthentication.Decrypt(cookie.Value);
string CookieValue = formAuthTicket.UserData.ToString();
<text> welcome <b> @Html.Label(CookieValue)</b>!
[@Html.ActionLink("Log off", "logout", "Members", new { area = "Members" }, null)]
</text>
}
else
{
<text>Welcome Guest!</text>
@:[ @Html.ActionLink("Log in", "Login", "Members", new { area = "Members" }, null)]
}
but it doesn’t work and shows error on this line:
var formAuthTicket = FormsAuthentication.Decrypt(cookie.Value);
error:
Invalid value for ‘encryptedTicket’ parameter.
what should i do?
i want to show username top of all page, and all username’s db values in personal page of him.
and he will be log on and surf all page until he sign out.
If you write the authentication cookie using FormsAuthentication, you do not need to decrypt and read the raw cookie value. You can just use
@User.Identity.Namein your views.This is what SetAuthCookie looks like internally:
Notice that it does in fact create a forms authentication ticket and encrypts the cookie,