我们可以用表单认证模块中的自定义对象替换表单认证票证吗?

时间:2013-05-08 03:21:35

标签: asp.net form-authentication

在我的项目中,需要使用自定义对象来代替FormAuthenticationTicket。我们创建了一个与FormAuthenticationTicket具有相同属性的自定义对象。现在我们使用自己的加密方法来加密这个自定义对象。我们已经成功创建了FormAuthentication cookie。但是当我们检查context.User.Identity.IsAuthenticated属性时,它始终为false。这是代码。

    CookieData cookieData = new CookieData();
    cookieData.ExpirationDate = DateTime.Now.AddMinutes(CookieConstants.DefaultFormsAuthTicketTimeout);
    cookieData.LoginToken = LoginToken;
    cookieData.Impersonate = (IsImpersonate ? true : false);
    cookieData.IsPersistent = IsPersistent;
    cookieData.UserData = "<<UserRelated Information>>";
    JavaScriptSerializer js = new JavaScriptSerializer();
    string strCookieData = js.Serialize(cookieData);
    string encryptedTicket = AESEncryption.Encrpyt(strCookieData);

     HttpCookie httpCookie = null;
     httpCookie = new HttpCookie(cebCookie.Name, encryptedTicket);



        if (cebCookie.IsPersistent)
        {
            httpCookie.Expires = cebCookie.Expires;
        }

        if (!string.IsNullOrEmpty(cebCookie.Url))
        {
            httpCookie.Secure = cebCookie.RequireSSL;
            httpCookie.Domain = cebCookie.Domain;
        }
        httpCookie.Path = FormsAuthentication.FormsCookiePath;
        HttpContext.Current.Response.Cookies.Set(httpCookie);

我知道这是有线要求。 请帮忙。

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的要求,您需要使用身份验证票证发送自定义数据吗?

如果您可以像下面这样做。

想象一下,您需要发送以下数据。

public class AuthUser
    {
        public int UserID { get; set; }
        public string UserNo { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public override string ToString()
        {
            return UserID + "," + UserNo + "," + UserName + "," + Password; 
        }
    }

在“登录”按钮中单击“事件”。

 AuthUser au = new AuthUser();
 au.UserID = 1;
au.UserNo = "001";
 au.UserName = "chamara";
au.Password = "123";

 string userData = au.ToString();

  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(

             2,                             // Version number

             txtUserName.Text.Trim(),      // Username

             DateTime.Now,                  // Issue date

             DateTime.Now.AddDays(555), // Expiration date

             false,                         // Persistent?

             userData                 // User data

         );

检索用户数据

FormsIdentity id = (FormsIdentity)Context.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string[] UserData = ticket.UserData.Split(',');