Asp.net mvc 5 在MVC5应用程序中使用会话是最好的方法
我正在尝试在asp.NETMVC5应用程序中实现会话。应用程序没有登录屏幕。应用程序检查数据库中是否存在访问应用程序的用户。在会话中捕获活动的控制器用户名,并将其发送到storedproc以验证该用户是否存在。如果存在,我需要在会话中存储Userprofile信息。我已经创建了一个存储库类来访问数据。我正在从global.asax中的session start方法调用该方法。我想验证我的实现是否正确。如果信息发生更改,如何更新会话数据 MCRHelperAsp.net mvc 5 在MVC5应用程序中使用会话是最好的方法,asp.net-mvc-5,Asp.net Mvc 5,我正在尝试在asp.NETMVC5应用程序中实现会话。应用程序没有登录屏幕。应用程序检查数据库中是否存在访问应用程序的用户。在会话中捕获活动的控制器用户名,并将其发送到storedproc以验证该用户是否存在。如果存在,我需要在会话中存储Userprofile信息。我已经创建了一个存储库类来访问数据。我正在从global.asax中的session start方法调用该方法。我想验证我的实现是否正确。如果信息发生更改,如何更新会话数据 MCRHelper public static strin
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;
}
}
我想验证我的实现是否正确
首先,您不应该将登录用户的信息存储在会话状态中更不用说在ASP.NET MVC中尽可能不鼓励使用会话状态
在15年前的ASP.NET成员资格提供程序之前,我们通常以会话状态存储登录的用户信息
由于您使用的是ASP.NET MVC 5,因此希望使用ASP.NET OWIN Cookie中间件。实现比您想象的要容易得多
然后可以开始在控制器和操作方法中使用[Authorize]
属性
[Authorize]
public class UsersController : Controller
{
// ...
}
下面是使用AD进行身份验证的示例。我有用户友好型,但如果您不想使用它,则不必使用。无登录屏幕。。。您如何获得有关用户的任何信息?数据模型显示:姓名、姓氏、电子邮件地址。。。要更新数据,您可以检查会话对象中是否已经存在该数据:更新它,否则创建一个新条目。MCRHelper.GetShortname()做什么?我已经用该方法更新了帖子。just从字符串中获取ntlogon用户名这是利用Windows身份验证吗?用户信息在GetShortName方法中捕获,该方法使用HttpContext.Current.User.Identity.Name获取当前主体的身份。您能告诉我为什么使用会话不好,因为我的应用程序将在负载平衡的环境中运行吗负责会话管理的环境。在我的解决方案中,我从数据库中读取用户配置文件信息,并尝试将其存储在会话中。如何在cookie中存储这些信息。Http是无状态的,ASP.NET MVC试图保持这种方式,因为在ASP.NET Web表单中维护会话状态有很多问题。正如我所说的,以会话状态存储用户信息是一种有15年历史的方法,非常脆弱且难以维护,我们现在有了更好的解决方案,如ASP.NET Identity和OWIN中间件。此外,如果使用会话状态,则无法利用ASP.NET MVC的内置授权属性。如果您以后决定迁移到ASP.NET Core,现在就可以使用OWIN轻松地完成迁移。
private readonly HttpContextBase _context;
private const string AuthenticationType = "ApplicationCookie";
public OwinAuthenticationService(HttpContextBase context)
{
_context = context;
}
public void SignIn(User user)
{
IList<Claim> claims = new List<Claim>
{
new Claim(ClaimTypes.Sid, user.Id.ToString()),
new Claim(ClaimTypes.Name, user.UserName),
new Claim(ClaimTypes.GivenName, user.FirstName),
new Claim(ClaimTypes.Surname, user.LastName),
};
ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);
IOwinContext context = _context.Request.GetOwinContext();
IAuthenticationManager authenticationManager = context.Authentication;
authenticationManager.SignIn(identity);
}
public void SignOut()
{
IOwinContext context = _context.Request.GetOwinContext();
IAuthenticationManager authenticationManager = context.Authentication;
authenticationManager.SignOut(AuthenticationType);
}
[assembly: OwinStartup(typeof(YourApplication.Startup))]
namespace YourApplication
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ApplicationCookie",
LoginPath = new PathString("/Account/Login")
});
}
}
}
[Authorize]
public class UsersController : Controller
{
// ...
}