使用ActionFilterAttribute
做用户的验证
示例代码
public class SubScribeFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string code = filterContext.HttpContext.Request.QueryString["code"];
object id = filterContext.RouteData.Values["id"];
if (id != null)
{
try
{
OAuthAccessTokenResult result = OAuthApi.GetAccessToken(WxConfig.AppId, WxConfig.AppSecret, code);
if (result != null)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Weixin", action = "StudentInfo", openId = result.openid }));
}
else
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Weixin", action = "Error", id = "errorcode" }));
}
}
catch(Exception ex)
{
}
}
else
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Weixin", action = "Error", id = "nosubsribe" }));
}
base.OnActionExecuting(filterContext);
}
}