Asp.net mvc 从Web Api中保留令牌的位置
我有一个网站像:(用ASP.NETWebAPI编写),还有一个网站像:(用ASP.NETMVC编写)。 消费者将用户名和密码传递给这个webapi,它给了我一个令牌Asp.net mvc 从Web Api中保留令牌的位置,asp.net-mvc,asp.net-web-api,Asp.net Mvc,Asp.net Web Api,我有一个网站像:(用ASP.NETWebAPI编写),还有一个网站像:(用ASP.NETMVC编写)。 消费者将用户名和密码传递给这个webapi,它给了我一个令牌 { "access_token": "XuGGwTfZ_XvapFtwsFO0-...", "token_type": "bearer", "expires_in": 86399 } 我的问题是,我应该在消费者应用程序中将此令牌存储在哪里? 在消费者网站中,我有如下行为: public ActionResult Log
{
"access_token": "XuGGwTfZ_XvapFtwsFO0-...",
"token_type": "bearer",
"expires_in": 86399
}
我的问题是,我应该在消费者应用程序中将此令牌存储在哪里?
在消费者网站中,我有如下行为:
public ActionResult Login(LoginUserViewModel data)
{
var response = PostAsync<LoginUserResultViewModel>("http://webapi.com/token",
new StringContent("grant_type=password"+
"&username=" + data.Username +
"&password=" + data.Password,
Encoding.UTF8,
"application/x-www-form-urlencoded"));
HttpContext.Session[BLConstants._TOKEN_SESSION_NAME_] = response.Token;
FormsAuthentication.SetAuthCookie(data.Username, createPersistentCookie: true);
return View();
}
公共操作结果登录(LoginUserViewModel数据)
{
var响应=后同步(“http://webapi.com/token",
新建StringContent(“授权类型=密码”+
“&username=“+data.username+
“&password=“+data.password,
Encoding.UTF8,
“application/x-www-form-urlencoded”);
HttpContext.Session[BLConstants.\u TOKEN\u Session\u NAME\u]=response.TOKEN;
FormsAuthentication.SetAuthCookie(data.Username,createPersistentCookie:true);
返回视图();
}
在consumer中的另一个操作中,如:HomeController->GetList,我需要该令牌发送到webapi以获取列表,但我没有HttpContext.Session[Constants.\u令牌\u会话\u名称,\u],它为空
还有其他地方可以存放代币吗?将此令牌存储到会话中是否安全?您可以将信息存储在cookie中(最好是加密的),也可以使用您创建的自定义原则将其保存在用户HttpContext.Current.user中。并检查是否在调用第二个操作方法之间的某个位置将“HttpContext.Session[BLConstants.\u TOKEN\u Session\u NAME\u]”设置为“null”。它应该在第二个动作方法中可用。