Pergunta

My Put method is as follows:

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

But When I run my project and try to save my information then it throw me an exception "Object Reference is not set to the reference of an object"

I am using Web Api as my controller class.

After reading various documents I found that Api is stateless. Now how can I store my user information for further use.

Foi útil?

Solução

Use:

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


    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top