请教:如何在Asp.net MVC 中实现类似Servlet的登陆过滤,如下
public class loginFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse rsp=(HttpServletResponse)response;
HttpSession session=req.getSession(true);
String userName=(String)session.getAttribute("userName");
if(userName!=null)
{//验证通过
filterChain.doFilter(request, response);
}
else
{//验证不通过,取得设置的跳转页面参数
String noLogin=filterConfig.getInitParameter("noLogin");
rsp.sendRedirect(noLogin);
}
} catch (Exception sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
}
给方法或类加个[Authorize]特性