我想实现这个功能,如用户已登录,系统将执行某种操作;如果用户没有登录,他将被引导到登录页面,在登录成功后返回我不知道下面的语句是否正确    protected void btnAddToFavorite_Click(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.IsAuthenticated)
        {
            //do something
        }
        else
        {
            Response.Redirect("~/Login.aspx?ReturnUrl=" + HttpUtility.UrlEncode(PublicPar.HTMLPathOfRoot + "ShowProject.aspx?Type=1&ID=2"));
        }
    }

解决方案 »

  1.   

    判断sesion
    if(Session["user"]==null)
    {}
    forms验证
    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
      {
       HttpApplication app = (HttpApplication) sender;  
       HttpContext ctx = app.Context ; 
       if (ctx.User != null)
       {
        if (ctx.Request.IsAuthenticated == true)   
        {  
         System.Web.Security.FormsIdentity fi = (System.Web.Security.FormsIdentity)ctx.User.Identity ;  
         System.Web.Security.FormsAuthenticationTicket ticket = fi.Ticket ; 
         string userData = ticket.UserData;
         string[] roles = userData.Split (',') ; 
         ctx.User = new System.Security.Principal.GenericPrincipal (fi, roles) ; 
        } 
       }
      }
      

  2.   

    在page_load中
    if(Session["user"]==null)
    {
      Response.Redirect("login.aspx");
    }
    else
    {}
      

  3.   


    if(session["存的session名"] != null)
    {
             //登陆了
    }
    else
    {
            //未登陆
    }
      

  4.   

    请问用 if (HttpContext.Current.Request.IsAuthenticated) 判断对不对
      

  5.   

    请问用 if (HttpContext.Current.Request.IsAuthenticated) 判断对不对
      

  6.   

    方法是对的!你如果问了这么一个问题 用了那个方法 就建议你去msdn 查查forms身份验证的东西。
      

  7.   

    if(Session["user"]==null)
    {
      Response.Redirect("login.aspx");
    }
      

  8.   

    你还是用session 吧,form验证 有点麻烦,还需要配合 global,web.config