我想做的是在 API 中使用 HttpContext.Session
在同一个 WebApi Controller 但不同的操作中。
$.ajax({
url:baseAppUrl+"api/login",
type:"post",
data:{
action:"sms",
username:"",///mobile phone
},
success:function(ret,err){
}
它会给我一个代码,然后使用该代码登录。
但是在ajax再次发送reqeust之后我无法获得HttpContext.Session。
我正在使用 .net 核心 3.1
if (action.Equals("sms"))
{
if (string.IsNullOrEmpty(username))
{
return Json(new { Ok = false, Message = "parameter is null" });
}
Random rnd = new Random();
var verifyCode = rnd.Next(111111, 999999).ToString();
HttpContext.Session.SetString("code", verifyCode);
return Json(new { Ok = true, Message = verifyCode });
}
并在登录操作中:
if (action.Equals("mobileLogin"))
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(code))
{
return Json(new { Ok = false, Message = "paremeter is null" });
}
var userCode = HttpContext.Session.GetString("code"); //the userCode will be null.
if (!string.IsNullOrEmpty(userCode))
{
////.....
}
}
但是有了邮递员,一切就都解决了。
为什么?
