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.
redirect url
as in theSignIn
method. Now all looks good. Username is displayed in thelayout.cshtml
page in theThank/Index
view. Now if I try to refresh theThank/Index
, the Session gets null