WEB.CONFIG:
<authentication mode="Windows"></authentication>
*.CS
...
System.Security.Principal.WindowsImpersonationContext impersonationContext;
           impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
....
        impersonationContext.Undo();错误信息:"匿名标识不能执行模拟。 "
我设置了,<authentication mode="Windows"></authentication>还要怎么做呢?

解决方案 »

  1.   

    http://topic.csdn.net/t/20030127/16/1395727.html
      

  2.   

    当时为什么要用这个呢?其它地方用到了吗?IIS如何设置的?
      

  3.   

    http://blog.joycode.com/joe/archive/2004/11/29/39890.aspx.
    另外在WEB.CONFIG里面直接配置模拟,不在代码中写如何.这样可以指定要模拟的用户
      

  4.   

    代码:
    System.Security.Principal.WindowsImpersonationContext impersonationContext;
               impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
                FileStream fs0 = new FileStream(Server.MapPath(".") + "\\HOME\\register.txt", FileMode.Open, FileAccess.Read);
                //打开文本文件
                StreamReader sr0 = new StreamReader(fs0, Encoding.Default);
                string reg_info = sr0.ReadLine();
                sr0.Close();
                fs0.Close();
                if (reg_info != MACCPU_PASS)
                {                int counter;
                    FileStream fs = new FileStream(Server.MapPath(".") +"\\HOME\\counter.txt", FileMode.Open, FileAccess.Read);
                    //打开文本文件
                    StreamReader sr = new StreamReader(fs, Encoding.Default);
                    string num = sr.ReadLine();
                    if (num == "")
                        counter = 0;
                    else
                        counter = Int32.Parse(num);
                    sr.Close();
                    fs.Close();
                    if (counter <= 50)
                    {
                        int counter_new = counter + 1;
                        fs = new FileStream(Server.MapPath(".") +"\\HOME\\counter.txt", FileMode.Create, FileAccess.Write);
                        //建立StreamWriter为写做准备
                        StreamWriter rw = new StreamWriter(fs, Encoding.Default);
                        //使用WriteLine写入内容
                        rw.WriteLine(counter_new.ToString());
                        //将缓冲区的内容写入文件
                        //关闭rw对象
                        rw.Close();
                        fs.Close();
                    }
                    else
                    {
                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolmanage2005ConnectionString"].ConnectionString); 
                        SqlCommand comm = new SqlCommand("delete from Role_user", conn);
                        conn.Open();
                        comm.ExecuteNonQuery();
                        conn.Close();
                        Response.Redirect("~/HOME/xueke.aspx");
                    }
                }
                impersonationContext.Undo();
      

  5.   

    之所以要用模拟是因为要读写文件,IIS只是启用匿名访问,其它的都是默认的(没有选择)
      

  6.   

    to scow:看不太懂,明白点可以吗?
      

  7.   

    那个链接提供了完整的模拟代码,拷贝就可以用,效果是模拟后改变了WindowsIdentity.GetCurrent(),
    关于web.config:
    <identity impersonate="true" username="" password=""/>
    配置节我凭记忆写的,最好查询msdn确认