In my controller, I am populating certain fields with data.
public string AjaxLogin()
{
//some code to check admin or not
Session["UserName"] = "Smith";
if(type="Admin")
{
Session["UserRole"] = 1;
}
Session["EmployeeID"] = 101;
}
I have made an ajax request to this controller as shown below, and upon success, I need to access the session variables within the success function in order to verify the user's role.
$.ajax(
{
url: GLOBAL.GetAppPath() + 'Home/AjaxLogin',
data: data,
type: 'POST',
error: function (xhr, status, error) {
console.log(error);
},
success: function (result, status, xhr) {
if (result == 'OK')
{
var UserVal = '@Session["UserRole"]';
alert(UserVal);
if(UserVal == 1)
{
var baseUrl ="@Url.Action("Admin","AdminPage")";
window.location.href = baseUrl;
}
else
{
var baseUrl ="@Url.Action("Admin","RegularPage")";
window.location.href = baseUrl;
}
}
else {
$('#msgError').html('Error: ' + result);
$('#msgError').css('display', 'block');
}
},
});
However, I am unable to retrieve this variable within this call. I aim to validate the user role variable and redirect to appropriate URL actions based on its value.