gpt4 book ai didi

asp.net-mvc-5 - 在 MVC5 应用程序中使用 session 的最佳方法

转载 作者:行者123 更新时间:2023-12-04 01:46:56 25 4
gpt4 key购买 nike

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

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

最佳答案

I would like to verify if my implementation is correct.

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

在 15 年前的 ASP.NET Membership Provider 出现之前,我们曾在 session 状态中存储登录用户信息。

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

OwinAuthenticationService

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

Startup.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")
});
}
}
}

然后您可以开始在 Controller 和 Action 方法中使用 [Authorize] 属性。

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

这是 my sample application at GitHub它使用 AD 进行身份验证。我有用户友好 login screen ,但如果您不想要它,则可以不使用它。

关于asp.net-mvc-5 - 在 MVC5 应用程序中使用 session 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43048841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com