I have two projects - an MVC project and the other an API project. I have placed the login form and script in the MVC project and the back end is in the API project.
I have written the login form as below:
<form>
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="Username">
</div>
<div class="form-group">
<input type="password" class="form-control" id="pwd" placeholder="Password">
</div>
<button id="buttonSubmit" class="btn btn-default">LOG IN</button>
</form>
The script for login submit when a customer has filled in the above form:
var user =
{
UserName: $("#email").val(),
Password: $("#pwd").val(),
IsRemember: $(".customCheckBox").val()
}
$.ajax({
type: "POST",
url: "http://localhost:55016/api/ajaxapi/loginmethod",
data: user,
success: function (response) {
document.cookie = "UserName = " + response.UserName;
}
});
Then I have created session using API project as below:
[HttpPost]
[Route("api/ajaxapi/loginmethod")]
public UserValuesForLogOn AjaxLogOnMethod(UserValuesForLogOn user)
{
HttpContext.Current.Session["authToken"] = user;
return user;
}
After logged in I have called ajax post to get details as below, which is in the MVC project:
$.ajax({
type: "POST",
url: "http://localhost:55016/api/ajaxapi/caselistmethod",
success: function (response) {
}
});
Then I have written code in the API project to take session value as stored while login process:
[HttpPost]
[Route("api/ajaxapi/caselistmethod")]
public List<UserValuesForLogOn> AjaxCaseListMethod()
{
userDetails = (UserValuesForLogOn)HttpContext.Current.Session["authToken"];
return userDetails;
}
Both cookie and session values can't take in API project. Please help me. Is it possible to access session and cookie in a cross domain situation.
Thanks.