问题
I am using FormsAuthentication
for my current project. I am facing an issue. I am saved some user information to FormsAuthenticationTicket.UserData
string its working fine if user just login and redirect to main page, but if user check Remember Me checkbox and login again then i am getting UserData as a emoty string.
Here i am creating Authentication ticket after Login
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));
}
}
this is my custom class to extract User Data String and provide my current user information
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"]);
}
}
}
this is where i am trying to access the UserData and getting empty string
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);
}
}
}
}
Please let me know if i am doing something wrong
来源:https://stackoverflow.com/questions/20816425/getting-formsauthentication-ticket-userdata-as-empty-string