存储数据到客户端的最佳方式

时间:2014-02-22 11:37:21

标签: session asp.net-web-api asp.net-mvc-5 session-state

我的Put方法如下:

public void Post([FromBody]RavenUserView view)
        {
            if (ModelState.IsValid)
            {
                var request = new CreateUserRequest();
                request.ID = view.ID;
                request.Name = view.Name;
                request.UserName = view.UserName;
                request.Password = EncryptionDecryption.EncryptString(view.Password);//Encrypt The Password
                request.Email = view.Email;
                request.Phone = view.Phone;
                request.Country = "x";
                request.Note = "y";
                request.IsActive = view.IsActive;
                request.Creator = view.Creator;
                request.CreationDate = DateTime.UtcNow;
                request.ModificationDate = DateTime.UtcNow;
                request.Remarks = "z";
                var response = _facade.CreateUser(request);

                SaveUserDetailsToCookie(response.RavenUser.ID, response.RavenUser.UserName, EncryptionDecryption.DecryptString(response.RavenUser.Password));//Cookie should be stored Decrypted Format
                HttpContext.Current.Session[SessionDataKey.UserId.ToString()] = response.RavenUser.ID.ToString();
                HttpContext.Current.Session[SessionDataKey.UserName.ToString()] = response.RavenUser.UserName;
            }
        }

但是当我运行我的项目并尝试保存我的信息时,它会抛出异常“对象引用未设置为对象的引用”

我使用Web Api作为我的控制器类。

在阅读各种文件后,我发现Api是无国籍的。现在我如何存储我的用户信息以供进一步使用。

1 个答案:

答案 0 :(得分:2)

使用:

    public void Post([Bind(Include = "ID,Name,UserName,....")] CreateUserRequest request)
    {
        if(ModelState.IsValid)
        {
            var response = _facade.CreateUser(request);

            SaveUserDetailsToCookie(response.RavenUser.ID, response.RavenUser.UserName, EncryptionDecryption.DecryptString(response.RavenUser.Password));//Cookie should be stored Decrypted Format
            HttpContext.Current.Session[SessionDataKey.UserId.ToString()] = response.RavenUser.ID.ToString();
            HttpContext.Current.Session[SessionDataKey.UserName.ToString()] = response.RavenUser.UserName;
        }


    }