基本用户身份验证
/// <summary> /// 基本用户身份验证(各系统可根据自己的实际需要,自定义验证方法,此多语言模块实现了验证并提供参考依据) /// 详细说明:判断用户是否登录,如果未登录则跳转到统一登录页面 /// 其它:统一身份认证服务地址:http://218.201.35.212:11007/ /// 创建人:张甫军 创建时间:2017年11月28日 16:24 /// </summary> public class AuthAttribute : ActionFilterAttribute { /// <summary> /// /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { var loginID = string.Empty; var accessToken = string.Empty; //判断请求参数中是否带有登录账号 if (filterContext.HttpContext.Request.QueryString[Consts.LoginID] != null) { loginID = filterContext.HttpContext.Request.QueryString[Consts.LoginID]; filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(Consts.LoginID, loginID)); } //判断请求参数中是否带有访问令牌 if (filterContext.HttpContext.Request.QueryString[Consts.AccessToken] != null) { accessToken = filterContext.HttpContext.Request.QueryString[Consts.AccessToken]; filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(Consts.AccessToken, accessToken)); } //读取客户端存储的登录账号 if (filterContext.HttpContext.Request.Cookies[Consts.LoginID] != null) { loginID = filterContext.HttpContext.Request.Cookies[Consts.LoginID].Value; } //读取客户端存储的访问令牌 if (filterContext.HttpContext.Request.Cookies[Consts.AccessToken] != null) { accessToken = filterContext.HttpContext.Request.Cookies[Consts.AccessToken].Value; } //如果客户端没有登录账号或访问令牌,则需要到统一登录页面进行登录 if (string.IsNullOrWhiteSpace(loginID) || string.IsNullOrWhiteSpace(accessToken)) { //跳转到统一登录页面 //组装参数,并跳转到统一登录页面 var returnUrl = filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.Url.ToString()); var redirectUrl = string.Concat(Consts.LoginUrl, "?", "appId=" + Consts.AppID, "&secret=" + Consts.AppSecret, "&languageCulture=zh-CN", "&returnUrl=", returnUrl); if (filterContext.HttpContext.Request.Url != null) filterContext.Result = new RedirectResult(redirectUrl); } else { // accessToken = "PylR7Y/zASQNaogV3Q0mpVNcga3TzZxrqqFty34lbr/8CQj/lE3RZg6P7RzWpqZTeo9G++V636/LtstiqZ1pT7ODdPtal6egNCTKn9WG3bE="; //调用车联网统一身份认证服务。 var client = new IovAuthrizeClient(); var response = client.HttpRequest("api/Passport/VerifyToken", EnumHttpMethod.POST, new { AccessToken = accessToken }); //反序列化响应消息 var commonResult = JsonConvert.DeserializeObject<CommonResultResponse>(response); if (commonResult == null) { } } base.OnActionExecuting(filterContext); } }