日期:2014-05-18 浏览次数:20531 次
#region 存 Session["UserName"] = "登录用户名"; #endregion #region 读 string userName=(Session["UserName"]??"").ToString(); #endregion
------解决方案--------------------
是不是用户登录后就可以看那些文件的内容了嘛,如果是的话可以使用Form认证
在你的项目中建一个这样的类
/// <summary>
/// 用户身份认证实体类
/// </summary>
[Serializable]
public class UserIdentity
{
private int _ID;
/// <summary>
/// 用户ID
/// </summary>
public int ID
{
get { return _ID; }
set { _ID = value; }
}
private string _UserName;
/// <summary>
/// 用户登录名
/// </summary>
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private string _Name;
/// <summary>
/// 用户姓名
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Password;
/// <summary>
/// 用户密码
/// </summary>
public string Password
{
get { return _Password; }
set { _Password = value; }
}
private string _Roles;
/// <summary>
/// 用户拥有的角色
/// </summary>
public string Roles
{
get { return _Roles; }
set { _Roles = value; }
}
/// <summary>
/// 获取当前用户身份认证实体类
/// </summary>
public static UserIdentity GetUserIdentity()
{
HttpContext context = HttpContext.Current;
try
{
string strUser = ((FormsIdentity)context.User.Identity).Ticket.UserData;
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Deserialize<UserIdentity>(strUser);
}
catch (Exception)
{
return null;
}
}
}
你具体需要什么属性你自己增加或删除
然后在下面的代码写在你的登录页面中当用户登录成功后执行的代码段中
#region 将用户信息保存入cookie
UserIdentity userIdentity = new UserIdentity();
userIdentity.ID = user.ID;
userIdentity.Name = user.UserName.Trim();
userIdentity.UserName = user.LoginName.Trim();
userIdentity.Password = password.Trim();
userIdentity.Roles = "";
string strUser = new JavaScriptSerializer().Serialize(userIdentity);
HttpRequest request = HttpContext.Current.Request;
HttpResponse response = HttpContext.Current.Response;
//设置Ticket信息
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userIdentity.Name