我如何找到新注册用户的cookie并在部分视图MVC3中使用其值



我有两种方法:一种用于注册新用户 addNewuser(MemberRegisterModel mm),另一种用于为他创建cookie createCookie(MemberSloginModel Members)。我想使用注册后创建的cookie,以显示所有页面的用户名,直到他登录为止。

我追踪了我的代码,并看到了Cookie是创建的。我在 headerPartial.cshtml 中使用此代码。

<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)]
}

,但它不起作用,并在此行上显示错误:

    var formAuthTicket = FormsAuthentication.Decrypt(cookie.Value);

错误:

" EncryptedTicket"参数的无效值。

我该怎么办?我想在所有页面上显示用户名的顶部,以及在他的个人页面中所有用户名的数据库值。他将登录并冲浪所有页面,直到他签约为止。

如果使用FormaUthentication编写身份验证cookie,则无需解密并读取原始cookie值。您可以在视图中使用@User.Identity.Name

public ActionResult AddNewUser(MemberRegisterModel mm)
{
    ...
    FormsAuthentication.SetAuthCookie(mm.UserName, true || false);
    ...
    return Redirect("Index", "Home");
}

Hello, and welcome, <strong>@User.Identity.Name</strong>

这就是setauthcookie内部的样子:

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;
}

请注意,实际上它确实创建了一个表单身份验证票并加密cookie,

最新更新