在MVC5应用程序中使用session最佳方法

时间:2017-03-27 14:14:30

标签: asp.net-mvc-5

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

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

1 个答案:

答案 0 :(得分:4)

  

我想验证我的实施是否正确。

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

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

由于您使用的是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
{
   // ...
}

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