在 MVC5 应用程序中使用会话的最佳方法



我正在尝试在 mvc 5 应用程序中实现会话 asp.net。应用程序没有登录屏幕。应用程序检查数据库中是否存在访问该应用程序的用户。活动控制器用户名在会话中捕获并发送到存储过程以验证用户是否存在。如果存在,我需要在会话中存储用户配置文件信息。 我创建了一个存储库类来访问数据。我正在从 global.asax 中的会话启动方法调用该方法。我想验证我的实现是否正确。如果信息发生更改,如何更新会话数据。

麦克尔珀

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;
}
}

全球

protected void Session_Start(object sender, EventArgs e)
{
// set SessionUtil.User here
SessionRespository sessionRespository = new SessionRespository();
Session["UserProfile"] = sessionRespository.GetUserProfileByNetworkId();
}
我想

验证我的实现是否正确。

首先,您不应该将登录用户的信息存储在会话状态中,更不用说如果可能的话 ASP.NET 不鼓励在 MVC 中使用会话状态。

我们曾经在 15 年前的会员提供程序之前将登录的用户信息存储在会话状态 ASP.NET。

由于您使用的是 ASP.NET MVC 5,因此您希望使用ASP.NET OWIN Cookie 中间件。实施比您想象的要容易得多。

欧文认证服务

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);
}

启动.cs

您还需要配置启动才能发生所有这些情况。

[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]属性。

[Authorize]
public class UsersController : Controller
{
// ...
}

这是我在GitHub上的示例应用程序,它使用AD进行身份验证。我有用户友好的登录屏幕,但如果您不想要它,则无需使用它。

最新更新