Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:38:29+00:00 2026-06-13T01:38:29+00:00

i have two methodes : one for registering new user AddNewUser(MemberRegisterModel mm) , and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:38:31+00:00Added an answer on June 13, 2026 at 1:38 am

    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.Name in your views.

    public ActionResult AddNewUser(MemberRegisterModel mm)
    {
        ...
        FormsAuthentication.SetAuthCookie(mm.UserName, true || false);
        ...
        return Redirect("Index", "Home");
    }
    
    
    Hello, and welcome, <strong>@User.Identity.Name</strong>
    

    This is what SetAuthCookie looks like internally:

    public static void SetAuthCookie(string userName, bool createPersistentCookie)
    {
        FormsAuthentication.Initialize();
        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie, 
            FormsAuthentication.FormsCookiePath);
    }
    
    public static void SetAuthCookie(string userName, bool createPersistentCookie, 
      string strCookiePath)
    {
      FormsAuthentication.Initialize();
      HttpContext current = HttpContext.Current;
      if (!current.Request.IsSecureConnection && FormsAuthentication.RequireSSL)
        throw new HttpException(System.Web.SR.GetString("Connection_not_secure_creating_secure_cookie"));
      bool flag = CookielessHelperClass.UseCookieless(current, false, FormsAuthentication.CookieMode);
      HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie, flag ? "/" : strCookiePath, !flag);
      if (!flag)
      {
        HttpContext.Current.Response.Cookies.Add(authCookie);
        current.CookielessHelper.SetCookieValue('F', (string) null);
      }
      else
        current.CookielessHelper.SetCookieValue('F', authCookie.Value);
    }
    
    private static HttpCookie GetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath, bool hexEncodedTicket)
    {
      FormsAuthentication.Initialize();
      if (userName == null)
        userName = string.Empty;
      if (strCookiePath == null || strCookiePath.Length < 1)
        strCookiePath = FormsAuthentication.FormsCookiePath;
      DateTime utcNow = DateTime.UtcNow;
      DateTime expirationUtc = utcNow.AddMinutes((double) FormsAuthentication._Timeout);
      FormsAuthenticationTicket ticket = FormsAuthenticationTicket.FromUtc(2, userName, utcNow, expirationUtc, createPersistentCookie, string.Empty, strCookiePath);
      string str = FormsAuthentication.Encrypt(ticket, hexEncodedTicket);
      if (str == null || str.Length < 1)
        throw new HttpException(System.Web.SR.GetString("Unable_to_encrypt_cookie_ticket"));
      HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, str);
      httpCookie.HttpOnly = true;
      httpCookie.Path = strCookiePath;
      httpCookie.Secure = FormsAuthentication._RequireSSL;
      if (FormsAuthentication._CookieDomain != null)
        httpCookie.Domain = FormsAuthentication._CookieDomain;
      if (ticket.IsPersistent)
        httpCookie.Expires = ticket.Expiration;
      return httpCookie;
    }
    

    Notice that it does in fact create a forms authentication ticket and encrypts the cookie,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two distinct methods - one deletes a user and another deletes
I have two methods, one called straight after another, which both throw the exact
All -- I have these two methods in one of my classes: public static
I have two methods, one that I use to convert an image to a
Can somebody explain me one thing. I have two methods in my controller :
I have two overloaded methods, one with an optional parameter. void foo(string a) {
I have these two methods on a class that differ only in one method
I have two ajax calls. Both are sync. Both call methods from one controller.
When I'm testing my simple geometric library, I usually have one or two methods
If I have a Spring Controller with two SEPARATE methods, one annotated by: @ExceptionHandler(Exception.class)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.