关于webapi获取登陆用户信息

先是用户登陆,将用户信息用guid对应存储再session中

var dsfs = Guid.NewGuid().ToString();
HttpContext.Current.Session[dsfs] = ss;

然后将数据返回到客户端,客户端存储在cookie中

function setCookie(cname, cvalue, exdays) { 
	var d = new Date();
	// d.setTime(d.getTime() + (exdays*1000));//设置过期时间是多少天后;
    // d.setTime(d.getTime() + (exdays*60*60*1000));//设置过期时间是多少小时;
    d.setTime(d.getTime() + (exdays * 60 * 60 * 1000));//设置过期时间是多少小时;
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
setCookie("sessionid", result.userid, 2);

然后用户客户端提交请求的时候都会带上cookie,然后这样获取(weapi)

var cookie = HttpContext.Current.Request.Cookies["sessionid"];
var session = HttpContext.Current.Session[cookie.Value];

mvc的话是这样

var cookie = HttpContext.Current.Request.Cookies["sessionid"];
usermessage session = HttpContext.Current.Session[cookie.Value] as usermessage;
    原文作者:图纸t
    原文地址: https://blog.csdn.net/asdfghjkl110292/article/details/95455969
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞