首页 新闻 会员 周边 捐助

Asp.net mvc “过滤”问题

0
悬赏园豆:5 [已解决问题] 解决于 2009-04-25 19:29

请教:如何在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());
         }
     }

问题补充: 这个跟Session存储有何关系,假如session非NULL的话是不是就可以登陆跳转了
waynelove的主页 waynelove | 初学一级 | 园豆:140
提问于:2009-04-24 21:14
< >
分享
最佳答案
0

给方法或类加个[Authorize]特性

Gray Zhang | 专家六级 |园豆:17610 | 2009-04-24 22:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册