/// <summary>
/// Attribute for power Authorize
/// </summary>
public class AuthorizeIgnoreAttribute : Attribute
{
public AuthorizeIgnoreAttribute()
{
}
}
/// <summary>
/// 判断是否登陆,同时可以获取用户登陆状态和基础信息
/// </summary>
public class AuthorizeAndLoginedAttribute : Attribute
{
public AuthorizeAndLoginedAttribute()
{
}
}
/// 方法执行前,如果没有登录就调整到Passport登录页面
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var noAuthorizeAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AuthorizeIgnoreAttribute), false);
if (noAuthorizeAttributes.Length > 0)
return;
base.OnActionExecuting(filterContext);
//可以登陆也可以不登陆
var noAuthorizeGetUserInfoAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AuthorizeAndLoginedAttribute), false);
if (noAuthorizeGetUserInfoAttributes.Length > 0)
{
if (!string.IsNullOrEmpty(token) && !string.IsNullOrWhiteSpace(token))
{
var loginInfo = GetLoginInfo(token);
if (loginInfo != null)
{
filterContext.RequestContext.HttpContext.Items["client_token"] = loginInfo;
return;
}
}
else
{
var loginInfo = new User.LoginInfo(-1, "");
filterContext.RequestContext.HttpContext.Items["client_token"] = loginInfo;
return;
}
}
else
{
//判断登陆
if (string.IsNullOrEmpty(token) || string.IsNullOrWhiteSpace(token))
{
//如果未登陆,先判断是否是商品搜索列表接口,如果是就把userid的值设为0
if (filterContext.RequestContext.RouteData.Values["action"].ToString() == "GetProSelect")
{
var loginInfo = new User.LoginInfo(0, "24fc7f23-ad33-4236-827e-11b830bba574");
filterContext.RequestContext.HttpContext.Items["client_token"] = loginInfo;
return;
}
else
{
var respons_json = new BY.Epack.APP.Interface.Model.ModelBase()
{
code = login_faile,
data = "",
msg = "没有登陆"
};
filterContext.Result = Json(respons_json, JsonRequestBehavior.AllowGet);
// return;
}
}
else
{
var loginInfo = GetLoginInfo(token);
if (loginInfo != null)
{
filterContext.RequestContext.HttpContext.Items["client_token"] = loginInfo;
return;
}
else
{
//登陆出错,需要重新登陆
var respons_json = new BY.Epack.APP.Interface.Model.ModelBase()
{
code = login_token_error,
data = "",
msg = "登陆异常,请重新登陆"
};
filterContext.Result = Json(respons_json, JsonRequestBehavior.AllowGet);
}
}
}
}