【发布时间】:2017-04-01 02:20:11
【问题描述】:
我正在尝试在 asp.net mvc 5 应用程序中实现会话。该应用程序没有登录屏幕。应用程序检查访问该应用程序的用户是否存在于数据库中。 Active Director 用户名在会话中被捕获并发送到存储过程以验证用户是否存在。如果存在,我需要将 Userprofile 信息存储在会话中。我创建了一个存储库类来访问数据。我从 global.asax 中的会话启动方法调用该方法。我想验证我的实现是否正确。如果信息发生变化,如何更新会话数据。
MCRHelper
public static string GetShortname()
{
string username = HttpContext.Current.User.Identity.Name;
return username.Split('\\')[1];
}
型号
[Serializable]
public class UserProfileSessionData
{
public int UserProfileID { get; set; }
public int EmployeeID { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string PreferredName { get; set; }
public string DefaultLanguageCode { get; set; }
public string DefaultCountryCode { get; set; }
public int TimeZoneID { get; set; }
public string TimeZoneName { get; set; }
public string Domain { get; set; }
public string NetworkID { get; set; }
public string EmailAddress { get; set; }
}
存储库类
public class SessionRespository
{
public List<UserProfileSessionData> GetUserProfileByNetworkId()
{
MCREntities db = new MCREntities();
if (MCRHelper.UserValidate() == 1)
{
var userProfiles = db.spGetUserProfileByNetworkID(MCRHelper.GetShortname());
return Mapper.Map<List<UserProfileSessionData>>(userProfiles);
}
return null;
}
}
Global.asax
protected void Session_Start(object sender, EventArgs e)
{
// set SessionUtil.User here
SessionRespository sessionRespository = new SessionRespository();
Session["UserProfile"] = sessionRespository.GetUserProfileByNetworkId();
}
【问题讨论】:
-
没有登录屏幕...您如何获得有关用户的任何信息?数据模型显示:Forename、Surname、Emailadress... 要更新数据,您可以检查该数据是否已存在于 Session 对象中,如果存在:更新它,否则创建一个新条目。
-
MCRHelper.GetShortname() 有什么作用?
-
我已经用这个方法更新了帖子。只是从字符串中获取 ntlogon 用户名
-
这是在使用 Windows Auth 吗?
-
用户信息在 GetShortName 方法中被捕获,该方法使用 HttpContext.Current.User.Identity.Name 获取当前主体的身份
标签: asp.net-mvc-5