Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在会话中存储登录的用户信息_Asp.net Mvc_Session - Fatal编程技术网

Asp.net mvc 在会话中存储登录的用户信息

Asp.net mvc 在会话中存储登录的用户信息,asp.net-mvc,session,Asp.net Mvc,Session,我目前正在mvc中实现会话以存储登录的用户详细信息。我正在使用HttpContext.Current.user.Identity.Name检索用户信息,然后将该值传递给数据库以查看它是否存在。请注意,没有登录屏幕。如果用户存在,我将在会话中存储该值 如果您看到下面的代码,我将从会话开始事件调用MCRHeler类方法。从那以后,代码就很容易解释了 我想知道是否有任何关于实施的建议或建议。我也想知道我是否需要处理杀戮治疗,如果需要,如何进行 Global.asax protected void Se

我目前正在mvc中实现会话以存储登录的用户详细信息。我正在使用
HttpContext.Current.user.Identity.Name
检索用户信息,然后将该值传递给数据库以查看它是否存在。请注意,没有登录屏幕。如果用户存在,我将在会话中存储该值

如果您看到下面的代码,我将从会话开始事件调用MCRHeler类方法。从那以后,代码就很容易解释了

我想知道是否有任何关于实施的建议或建议。我也想知道我是否需要处理杀戮治疗,如果需要,如何进行

Global.asax

protected void Session_Start(object sender, EventArgs e)
{
    var userDetails = MCRHelper.UserDetails;

    if (userDetails != null)
        Response.RedirectToRoute("Default");
    else
        Response.Redirect("Message/Authorize",false);
}
MCRHelper
class:

public static UserDetails UserDetails
{
    get 
    {
        if (HttpContext.Current.Session["UserDetails"] == null)
        {
            if (IsUserExists())
            {
                var userSessionRepository = new SessionRepository();
                var contactDetails = userSessionRepository.GetUserProfileByNetworkId(GetShortname());
                HttpContext.Current.Session["UserDetails"] = contactDetails;
            }
        }

        return HttpContext.Current.Session["UserDetails"] as UserDetails;
    }
}

public static bool IsUserExists()
{
    using (var db = new MCREntities())
    {
        return Convert.ToBoolean(db.spUserExists(GetShortname(), GetDomain(), 1).FirstOrDefault().UserExists);
    }
}
会话存储库:

public class SessionRepository 
{
    public UserDetails GetUserProfileByNetworkId(string user)
    {
        MCREntities db = new MCREntities();

        var userProfiles = db.spGetUserProfileByNetworkID(user).FirstOrDefault(); 
        return Mapper.Map<UserDetails>(userProfiles);
    }
}
公共类SessionRepository
{
公共用户详细信息GetUserProfileByNetworkId(字符串用户)
{
McRenties db=新的McRenties();
var userProfiles=db.spGetUserProfileByNetworkID(user.FirstOrDefault();
返回Mapper.Map(userProfiles);
}
}

如何在没有登录屏幕/掩码/表单的情况下登录用户?此代码可以实现此目的。HttpContext.Current.User.Identity.Name。它获取窗口用户