补充一句:GetLastError得到的错误是:126
具体意思就是:缺少模块,缺少哪一个? The specified module could not be found.  

解决方案 »

  1.   

    see
    INFO: Implementing Impersonation in an ASP.NET Application
    http://support.microsoft.com/default.aspx?scid=kb;ZH-CN;Q306158
    also try (from http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=%23pFiEFy4BHA.1752%40tkmsftngp02&rnum=1&prev=/groups%3Fq%3Dasp.net%2Blogonuser%2BSE_TCB_NAME%2B126%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D%2523pFiEFy4BHA.1752%2540tkmsftngp02%26rnum%3D1)
    using System;
    using System.Runtime.InteropServices;public class WebForm1 : System.Web.UI.Page
    {
    [DllImport("C:\\Windows\\System32\\advapi32.dll")]
    public static extern bool LogonUser(String lpszUsername, String 
    lpszDomain, String lpszPassword, 
    int dwLogonType, int dwLogonProvider, out int phToken);
    [DllImport("C:\\Windows\\System32\\Kernel32.dll")]
    public static extern int GetLastError();
    private void Page_Load(object sender, System.EventArgs e)
    {
    int token1;  
    bool loggedOn = LogonUser(
    // User name.
    "junsu",    
    // Domain
    "Domain1",    
    // Password.
    "*****",   
    // Logon type = LOGON32_LOGON_NETWORK_CLEARTEXT.
    3,   
    // Logon provider = LOGON32_PROVIDER_DEFAULT.
    0,    
    // The user token for the specified user is returned here.
    out token1);            
          
    Response.Write("LogonUser called");
             
    // Call GetLastError to try to determine why logon failed if it did not succeed. int ret = GetLastError();
         
    Response.Write("LogonUser Success? " + loggedOn);
    Response.Write("NT Token Value: " + token1);
    } // some functions omit.
    }
      

  2.   

    飞刀兄,老弟正在看,有几个问题得你先回答我:
    1) 平台,在windows200和xp下,这个函数有些不同.
    2) LogonUser的格式
    3)GetLastError得位置,就紧挨着LogonUser吗?
    一个人钻往往比较慢,如果不介意的话,把你的源码发到[email protected]. 我还在研究,直到有人解决.
      

  3.   

    还是saucer(思归)兄厉害,不管是不是解决的飞刀兄的问题,起码找的比在下准,回答潜心再学习半个月,再来和大家学习.
    saucer(思归),佩服佩服.
      

  4.   

    怨不得saucer(思归)这么厉害,原来是7月的状元.哈哈,我出了个问题,有兴趣去看看码?
    http://www.csdn.net/expert/topic/1010/1010031.xml?temp=.6753961
      

  5.   

    我用KB上的代码试了一下,果然复现了你说的126 Error Code。然后我尝试用VC6 Debuger跟进去看LogonUser的参数是否正确,为此把aspnet_wp进程停掉了好几次,接着奇妙的事情发生了:),此后的所有LogonUser的调用都会成功!不论重起服务或者重起aspnet_wp进程。我想你肯定是先编辑machine.config或者其它相关文件来改变aspnet_wp的权限设置,然后再运行上述代码的,不妨试试看停掉www服务,kill掉aspnet_wp,然后重起一次服务,也许可以。另外,我注意到MSDN里的一个例子也有126 ErrCode的检测,但是没有交待怎样解决,看来碰到问题的不只是你我了。:)
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecurityprincipalwindowsimpersonationcontextclasstopic.asp//Whisper: 弄不好是.NET/Asp.net的bug。
      

  6.   

    read the small print from 
    http://support.microsoft.com/default.aspx?scid=kb;ZH-CN;Q306158".....
    The identity of the process that impersonates a specific user on a thread must have the "Act as part of the operating system" privilege. By default, the Aspnet_wp.exe process runs under a computer account named ASPNET. However, this account does not have the required privileges to impersonate a specific user. You receive an error message if you try to impersonate a specific user.To work around this problem, use one of the following methods: 
    1. Grant the "Act as part of the operating system" privilege to the ASPNET account.
    2. Change the account that the Aspnet_wp.exe process runs under to the System account in the <processModel> configuration section of the Machine.config file.
    ....."
    to grant the "Act as part of the operating system" privilege to the ASPNET account, go to Administrative Tools, open Local Security Policy, go to Local Policies->User Rights Assignment, right click on "Act as part of the operating system" in the right pane, select Security..., add ASPNET account to it, click on OK, restart your web server (use iisreset command in DOS prompt), re-try.... 
      

  7.   

    如果权限不够,报错会是1314:
    1314 
    A required privilege is not held by the client.  ERROR_PRIVILEGE_NOT_HELD
      

  8.   

    我没有测试过,不过下面白鸟(freesoul)的article可能对你有用http://www.microsoft.com/china/community/article.asp?oBODY=TechZone/TechArticle/TechDoc/impersonation&oXSLT=TechZone/TechArticle/TechContent
      

  9.   

    In the C# newsgroup of the MS, This is a question like yours, the return code is same.
      

  10.   

    to:saucer(思归)
    那段代码是MSDN上的,我查过了。在console与Winform中没有问题,现在是要用于Web,所以问题就来了:(to:larson(Unkonwn)
    就是因为了有了白鸟兄弟的代码,我才敢确认可以在Web中使用LogonUser,只是他的代码,返回亦是Falseto:TheAres(阿瑞斯) 
    发那个贴的人也是我...我是没有办法了,想在微软上,他们会回答吧..to:秋叶一知.
    你说的那段代码,和思归说的是一样的,是微软给出的标准格式。
    你那停aspnet_wp的方法还没有试,先去看一下:)万一不行,就直接改identity吧看看能不能把这个权限锁定在一个文件吧。
      

  11.   

    我虽然不太在行但我根据以上的对话
    分析得出这样的结论:
    既然可以在Console和WinFrom中运行
    那说名是没问题的,应该是ASP.NET里的工作方式有些不同
    不知道说的对不对!!!
      

  12.   

    if you grant the "Act as part of the operating system" privilege to the ASPNET account, you should be able to impersonate with the code in the above KB article. I tested it, it works fine
      

  13.   

    To: aspcn(飞刀)
    我在中文组里看到你的帖子.
    我说得是英文组里的.一个叫"Michael S. Collier"发的帖子.帖子的标题是"LogonUser() always returns 126". 他的问题已经被解决了. 你自己去搜搜看.
      

  14.   

    真是奇怪,我这里还是有时候出那个126的ErrCode。:(
    我是通过machine.config改aspnet_wp权限的,用SYSTEM不是Machine。如果改回去的话,报错的Error Code就是1314,权限不足。
      

  15.   

    我也觉得有些后悔.
    我要身份验证,就是为了有别人的权限来做事情.但是现在要使用LogonUser,就得给aspnet的帐号加权限,而这个权限就已经很大了...
    我测试了一下,加虚拟目录,删除文件等等,什么权限都有了.那我还要身份模拟干什么:(
      

  16.   

    好像MS在今后的Windows版本中(WindowXP...)取消了LogonUser对SYSTEM权限的要求。:)
    而且,我觉得Impersonation的目的也可以是缩小权限,在需要的最小权限集下工作安全性更好一些。所以可以这样理解,首先赋予aspnet系统权限,然后使用impersonation来限制它的权限到你真正需要的部分。