c# - 我正在尝试在 MVC 5 中使用 Session 来传递最近注册用户的 Id

标签 c# asp.net-mvc session asp.net-mvc-5 session-variables

我正在尝试使用 MVC 5 中的 session 来获取最近注册用户的 ID。 我有下面的代码,但它给我一个错误,提示“当前上下文中不存在名称“Session””

我使用 HttpContext.Current.Session 添加了这条语句;但它说“找不到类型或命名空间”我该怎么做才能解决这些错误?

public class RegisterBusiness
    {
        public UserManager<GlenwoodMed.Data.ApplicationUser> UserManager { get; set; }

        public RegisterBusiness()
        {
            UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new IdentityDataContext()));
        }

        public bool FindUser(string userName, IAuthenticationManager authenticationManager)
        {
            var user = UserManager.FindByName(userName);
            if (user != null)
            {
                return true;
            }
            return false;
        }

        public async Task<bool> RegisterUser(RegisterModel objRegisterModel, IAuthenticationManager authenticationManager)
        {
            var newuser = new ApplicationUser()
            {
                Id = objRegisterModel.UserName,
                UserName = objRegisterModel.UserName,
                Email = objRegisterModel.Email,
                Password = objRegisterModel.Password,
                FullName = objRegisterModel.FullName,
                Surname = objRegisterModel.Surname,
                MaritalStatus = objRegisterModel.MaritalStatus,
                DOB = objRegisterModel.DOB,
                Address = objRegisterModel.Address,
                PostalCode = objRegisterModel.PostalCode,
                Telephone = objRegisterModel.Telephone,
                Employer = objRegisterModel.Employer,
                EmployerTelephone = objRegisterModel.EmployerTelephone,
                Occupation = objRegisterModel.Occupation,
                NationalId = objRegisterModel.NationalId,
                Status = objRegisterModel.Status,
                MedicalAidName = objRegisterModel.MedicalAidName,
                MedicalAidNo = objRegisterModel.MedicalAidNo
            };

            var result = await UserManager.CreateAsync(
               newuser, objRegisterModel.Password);

            if (result.Succeeded)
            {

                //Session["id"] = newuser.Id;

                await SignInAsync(newuser, false, authenticationManager);
                return true;
            }
            return false;
        }

添加到它,我有一个类来处理名为 RegisterBusiness 的注册操作,它位于我名为 BusinessLogic 的解决方案下的类库中,因此 Session 使用 System.Web.mvc 命名空间,类库中不存在该类的 .dll我创建了我要添加到 BusinessLogic 类库的必要 .dll 是什么,因为我尝试只添加 System.Web.Mvc,但我认为它依赖于其他人,因为它目前还没有响应。你能进一步帮助吗?谢谢。

最佳答案

根据 MSDN 站点,如果您使用的是 HttpContext.Current.Session,请尝试以下代码:

HttpContext context = HttpContext.Current;
context.Session["id"] = newuser.Id;
var id = (string)(context.Session["id"]);

您可以存储除字符串以外的其他类型,只需将 session 转换为该类型即可。

根据您的代码:

Session["id"] = newuser.Id;

您可以在 Web 窗体页面类中使用它。

编辑:

HttpContext.Current.Session 在System.Web.Mvc 中。很可能您为我们呈现的这个类是在Web 页面之外的。我认为,您不能在 WebPage 类之外设置 session 。这可以在 fe 中完成。 Controller 使用更简单的方法:Session["id"] = "id"。

所以我的想法是不返回 True,而是返回 newuser.Id 并将其分配给调用此注册函数的类中的 session 。

编辑2: 上面的解决方案是当标准的

System.Web.HttpContext.Current.Session["Id"] = newuser.Id;

不起作用。首先尝试这个,但不要添加任何 Using 语句。您只需要 System.Web.Mvc;运行这个。

关于c# - 我正在尝试在 MVC 5 中使用 Session 来传递最近注册用户的 Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29792382/

相关文章:

c# - 获取嵌套在列表中的最大计数列表

c# - MVC 中用户过期的最佳位置

java - 如何在从移动客户端调用的 Spring REST Web 服务中创建和销毁 session

c# - TFS 构建失败

c# - 添加基于带有 SignedXml 类的 Id 属性的引用时出现“格式错误的引用元素”

asp.net-mvc - 对自定义 MVC html 控件使用分部 View

asp.net - 在 RedirectToAction 调用中传播 QueryString 参数

c# - 如何在 ASP.Net MVC 中访问 session 变量

java - Servlet 过滤器重定向,url 未更改

c# - ASP.NET Identity 默认的 ApplicationUserManager 是否实现了 IUserEmailStore?