0

On user sign in I'm calling the below sign in controller using angularjs $http post for sign in.

public JsonResult SignIn(string username, string password)
{
    password = PasswordEncryptionDecryption.Encrypt(password);

    User user = new User() { Username = username, Password = password };
    userBL = new UserBL(user);
    User response = userBL.ValidateUserLogin();
    response.ProfileImageUrl = ConfigurationManager.AppSettings["Profile"] + response.ProfileImageUrl;
    SessionManager.SetLoggedinuser(response);

    Session["LoggedInUser"] = response.PrimaryId;
    Session["LoggedInUserRole"] = response.UserRole;
    Session["LoggedInUserName"] = response.Name;
    Session["LoggedInProfileImageUrl"] = response.ProfileImageUrl;

    return Json(new { response = response, redirectUrl = "Thank/Index" });
}

The above controller sets the Session variables and returns JSON result along with the redirect url. I'm redirecting using JavaScript from client side. Here is the code:

$scope.SignIn = function () {
    $http({
        // set the parameter for request
        method: 'POST',
        url: 'Signin/SignIn',
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        },
        data: { username: $scope.user.username, password: $scope.user.password }
    })
    .then(function (resp) {
        //
        console.log(resp);
        if (resp.data.response.StatusCode == 200) {
            $window.location.href = rootUrl + resp.data.redirectUrl;
        } else {
            alert(resp.data.response.StatusMessage);
        }
    },
    function (error) {
        //
    });
});

I'm using the Session["LoggedInUserName"] in my _Layout.cshtml to display the logged in user name.

Here is the Thank Controller code :

public class ThankController : Controller
{
    private ThankYouNotes thankYouModel;
    private ThankYouBL thankYouBL;

    public ActionResult Index()
    {
        return View();
    }

    public JsonResult GetThankYouNotes()
    {
        thankYouBL = new ThankYouBL();
        List<ThankYouNotes> thankYouList = thankYouBL.GetThankYouNotes(SessionManager.CurrentLoggedinuser.PrimaryId);
        return Json(new { response = thankYouList });
    }
}

The issue is that on page refresh the Session becomes null and if I refresh further it gets set. I really can't figure what I'm doing wrong.

4
  • Once the user is validated, he is redirected to the redirect url as in the SignIn method. Now all looks good. Username is displayed in the layout.cshtml page in the Thank/Index view. Now if I try to refresh the Thank/Index, the Session gets null Commented May 18, 2016 at 6:48
  • can you please show your Index Action too? Commented May 18, 2016 at 6:59
  • @psyLogic updated the code in the question Commented May 18, 2016 at 7:01
  • I honestly see nothing in the above code that would cause the problem, there has to be something else which resets the session like some action getting called or something. Commented May 18, 2016 at 7:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.