将FormsAuthentication Ticket.UserData作为空字符串获取

时间:2013-12-28 15:59:04

标签: c# asp.net forms-authentication

我正在使用FormsAuthentication作为我当前的项目。我正面临一个问题。如果用户只是登录并重定向到主页,我将一些用户信息保存到FormsAuthenticationTicket.UserData字符串工作正常,但如果用户选中记住我复选框并再次登录,那么我将 UserData 作为情感串。

我在登录后创建身份验证票据

 private void CreateAuthenticationTicket()
        {
            var userInfo = new UserProvider().GetUserInfo(UserName);
            if (userInfo != null && userInfo.PersonalInfo != null)
            {
                var userFullName = string.Format("{0} {1}", userInfo.PersonalInfo.FirstName,
                    userInfo.PersonalInfo.LastName);
                //setting up user data string
                var userData = string.Format(GlobalFormats.UserDataStringFormat, userFullName,
                    Convert.ToString(userInfo.UserId), UserName, DateTime.Now,
                    userInfo.PersonalInfo.UserTypeId == (int) UserTypes.Administrator);

                HttpCookie authCookie = FormsAuthentication.GetAuthCookie(UserName, checkRememberMe.Checked);
                var ticket = FormsAuthentication.Decrypt(authCookie.Value);
                if (ticket != null)
                {
                    var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name,
                        ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userData);
                    authCookie.Value = FormsAuthentication.Encrypt(newTicket);
                    Response.Cookies.Add(authCookie);
                    Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, checkRememberMe.Checked));
                }
            }

这是我的自定义类,用于提取用户数据字符串并提供我当前的用户信息

public class CurrentUser{ 
    public CurrentUser(FormsIdentity identity)
            {
                if (identity == null)
                {
                    throw new UnauthorizedAccessException();
                }
                this.identity = identity;
                ExtactTickerInformationFromIdentity();
            }

            public CurrentUser(HttpCookie cookie)
            {
                if (cookie != null)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                    if (ticket != null)
                    {
                        ExtractUserInformationFromUserData(ticket.UserData);
                    }
                }
            }

    private void ExtractUserInformationFromUserData(string userData)
            {
                if (string.IsNullOrWhiteSpace(userData)) return;
                var valuesArray = userData.Split('|');
                if (valuesArray.Length == 0) return;
                var dictionary =
                    (from item in valuesArray let extracted = item.Split('#') select extracted).ToDictionary(i => i[0],
                        i => i[1]);
                if (dictionary.Keys.Count > 0)
                {
                    CurrentUserId = dictionary["UserId"];
                    CurrentUserName = dictionary["UserName"];
                    LoginDateTime = Convert.ToDateTime(dictionary["LoginDateTime"]);
                    UserEmail = dictionary["UserEmail"];
                    IsAdministrator = Convert.ToBoolean(dictionary["IsAdministrator"]);
                }
            }
}

这是我试图访问UserData并获取空字符串

的地方
private void DisplayUserName()
        {
            if (Master != null)
            {
                var lblControl = Master.FindControl("lblDisplayUserName") as Label;
                if (lblControl != null)
                {
                    HttpCookie cookie = Request.Cookies.Get(FormsAuthentication.FormsCookieName);
                    var userName = new CurrentUser(cookie).CurrentUserName;
                    if (!string.IsNullOrWhiteSpace(userName))
                    {
                        lblControl.Text = string.Format("Welcome {0}", userName);
                    }
                }
            }
        }

如果我做错了,请告诉我

0 个答案:

没有答案