简体   繁体   English

在asp.net mvc 中获取用户的会话ID

[英]Get user's session ID in asp.net mvc

In an asp.net mvc application, i would like to use a rest webservice to return the username associated with the session id passed as a parameter.在asp.net mvc 应用程序中,我想使用rest webservice 来返回与作为参数传递的会话ID 关联的用户名。

How do I get the sessionID of the logged in User ?如何获取登录用户的 sessionID?

Do I need to set the session ID Manually in my login method ?我是否需要在登录方法中手动设置会话 ID?

Also how can I get the user information using the session ID ?另外,如何使用会话 ID 获取用户信息?

Any help will be appreciated.任何帮助将不胜感激。 Thank you !谢谢 !

Login Method in Account Controller:账户控制器中的登录方法:

[HttpPost]
    public ActionResult LogIn(UserLoginView ULV, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            UserManager UM = new UserManager();
            string password = UM.GetUserPassword(ULV.EmailID);

            if (string.IsNullOrEmpty(password))
                ModelState.AddModelError("", "*The Email-ID or Password provided is incorrect");
            else
            {
                if (ULV.Password.Equals(password))
                {
                    FormsAuthentication.SetAuthCookie(ULV.EmailID, false);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "*The Password provided is incorrect");
                }
            }
        }

        return View();
    }

Web service method in User Controller:用户控制器中的 Web 服务方法:

 public class UserController : ApiController
    {
    [HttpGet]
    public string UserInfo()
    {
       HttpSessionState sessionValue = HttpContext.Current.Session;

        return sessionValue.SessionID.ToString();

    }
}

What version of .NET are you using?您使用的是什么版本的 .NET? Using .NET Framework 4.5.2 I was able to obtain the SessionID value using: string sessionID = HttpContext.Session.SessionID;使用 .NET Framework 4.5.2 我能够使用以下方法获取 SessionID 值: string sessionID = HttpContext.Session.SessionID;

链接链接中的第一个答案提供了获取会话 ID 的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM