我使用ASP.NET的Form验证
<anonymousIdentification enabled="true"/>
<authentication mode="Forms" >
  <forms name="yourAuthCookie" loginUrl="/Profile/SignIn.aspx"   protection="All" path="/Profile"  />
</authentication>
<authorization>
  <deny users="?"/>
</authorization>//Profile设置
<profile automaticSaveEnabled="false">
      <providers>
        <add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnectionString"/>
      </providers>
      <properties>
        <add name="FirstName" allowAnonymous="true"/>
      </properties>
</profile>登陆没有使用Membership,而是自定义的
 protected void SignIn_Click(object sender, EventArgs e) {
    if(txtUserName.Text.Trim() == "myname" && txtPWD.Text.Trim() == "mypwd"){
        addCookie();
        Response.Redirect("default.aspx");  
    }    
 } private void addCookie() {
    FormsAuthenticationTicket t = new FormsAuthenticationTicket("test", false, 30);
    string s = FormsAuthentication.Encrypt(t);
    HttpCookie hc = new HttpCookie(FormsAuthentication.FormsCookieName, s);
    Response.Cookies.Add(hc);
  }Global.asax中添加了匿名迁移函数
 void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs e) {       
        //这只是测试数据,为的是匿名迁移时,我们知道它触发了
        string testStr = Convert.ToString(Application["test"]);
        Application["test"] = testStr + "1";
        testStr = Convert.ToString(Application["test"]);
        HttpContext.Current.Response.Write(testStr +"<br/>");
    }
刚点击登陆按钮时会执行Global.asax中的Profile_MigrateAnonymous函数,即在default.aspx中输出1,但是随后的每一次操作都会执行Profile_MigrateAnonymous函数(可以看出输出的字符串在增加),包括每一个新打开的页面,在页面上的刷新操作都会执行Profile_MigrateAnonymous函数,
不明白为什么登陆后每次都执行Profile_MigrateAnonymous函数?该怎样解决这个问题?谢谢!

解决方案 »

  1.   

    你的目的是什么呀,Profile_MigrateAnonymous是匿名迁移函数,主要是把匿名profile迁移到认证profile状态,如果不是匿名状态不会执行这个函数的,好象是。如果想让函数里面的语句不执行,可以加个判断,是否和上一个状态一样,如果状态相同就不执行,否则执行。
      

  2.   

    Profile_MigrateAnonymous函数应该是在由匿名状态转向认证状态时才触发的,应该只在登陆时执行一次,为什么我认证后(即执行了addCookie()函数),每打开一次页面或刷新页面Profile_MigrateAnonymous函数都执行?PetShop4.0的Profile的例子只是在登陆时执行一次啊,不过它是用Membership登陆的
      

  3.   

    你的目的是什么呀,Profile_MigrateAnonymous是匿名迁移函数,主要是把匿名profile迁移到认证profile状态,如果不是匿名状态不会执行这个函数的,好象是。如果想让函数里面的语句不执行,可以加个判断,是否和上一个状态一样,如果状态相同就不执行,否则执行。
    好建议