问题描述
我使用类WindowsImpersonationContext。来模拟用户登录访问共享,但是用完后共享资源得不到及时释放,会出现会话空闲状态,不知道如何释放掉使用的示例是:我的代码如下:[DllImport("advapi32.dll",SetLastError=true)]privatestaticexternboolLogonUser(stringlpszUsername,stringlpszDomain,stringlpszPassword,intdwLogonType,intdwLogonProvider,refIntPtrphToken);[DllImport("kernel32.dll",CharSet=CharSet.Auto)]privateunsafestaticexternintFormatMessage(intdwFlags,refIntPtrlpSource,intdwMessageId,intdwLanguageId,refStringlpBuffer,intnSize,IntPtr*arguments);[DllImport("kernel32.dll",CharSet=CharSet.Auto,SetLastError=true)]privatestaticexternboolCloseHandle(IntPtrhandle);[DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)]publicexternstaticboolDuplicateToken(IntPtrexistingTokenHandle,intSECURITY_IMPERSONATION_LEVEL,refIntPtrduplicateTokenHandle);//logontypesconstintLOGON32_LOGON_INTERACTIVE=2;constintLOGON32_LOGON_NETWORK=3;constintLOGON32_LOGON_NEW_CREDENTIALS=9;//logonprovidersconstintLOGON32_PROVIDER_DEFAULT=0;constintLOGON32_PROVIDER_WINNT50=3;constintLOGON32_PROVIDER_WINNT40=2;constintLOGON32_PROVIDER_WINNT35=1;stringDomainName,User,Pass;publicShareAccess2(stringdomainname,stringuser,stringpass){DomainName=domainname;User=user;Pass=pass;}publicvoiddoworkinshare(stringsharefolder){IntPtrtoken=IntPtr.Zero;IntPtrdupToken=IntPtr.Zero;boolisSuccess=LogonUser(User,DomainName,Pass,LOGON32_LOGON_NEW_CREDENTIALS,LOGON32_PROVIDER_DEFAULT,reftoken);if(!isSuccess){RaiseLastError();}isSuccess=DuplicateToken(token,2,refdupToken);if(!isSuccess){RaiseLastError();}FileInfo[]files=null;using(WindowsIdentitynewIdentity=newWindowsIdentity(dupToken)){using(WindowsImpersonationContextwincontext=newIdentity.Impersonate()){//TODO:dosomethingDirectoryInfodirInfo=newDirectoryInfo(sharefolder);files=dirInfo.GetFiles();}}foreach(FileInfofileinfiles){Console.WriteLine(file.FullName);}isSuccess=CloseHandle(token);if(!isSuccess){RaiseLastError();}}//GetErrorMessageformatsandreturnsanerrormessage//correspondingtotheinputerrorCode.publicunsafestaticstringGetErrorMessage(interrorCode){intFORMAT_MESSAGE_ALLOCATE_BUFFER=0x00000100;intFORMAT_MESSAGE_IGNORE_INSERTS=0x00000200;intFORMAT_MESSAGE_FROM_SYSTEM=0x00001000;intmessageSize=255;stringlpMsgBuf="";intdwFlags=FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;IntPtrptrlpSource=IntPtr.Zero;IntPtrptrArguments=IntPtr.Zero;intretVal=FormatMessage(dwFlags,refptrlpSource,errorCode,0,reflpMsgBuf,messageSize,&ptrArguments);if(retVal==0){thrownewApplicationException(string.Format("Failedtoformatmessageforerrorcode'{0}'.",errorCode));}returnlpMsgBuf;}privatestaticvoidRaiseLastError(){interrorCode=Marshal.GetLastWin32Error();stringerrorMessage=GetErrorMessage(errorCode);thrownewApplicationException(errorMessage);}}
然后我循环调用该方法(每次用完应该是释放掉了资源,但是实际上会话还会保留几秒,不过那时候共享访问处于空闲状态,之后才会被释放。。。应该是系统自动释放的),到底应该如何释放掉共资源呢?
解决方案
解决方案二:
快帮忙啊
解决方案三:
该回复于2012-03-20 08:50:23被版主删除
解决方案四:
访问完共享资源后,用impersonationContext.Undo();返回原来的用户,即:DirectoryInfodirInfo=newDirectoryInfo(sharefolder);files=dirInfo.GetFiles();wincontext.Undo();
解决方案五:
非常感谢你的回答,但是问题依旧是那样啊,当我循环调用doworkinshare方法(我上面的代码)时,在vista或win7系统的共享中会出现多个用户访问的情况(原因应该是释放会话的时间太长,感觉像是系统在异步处理会话)并且处于空闲状态结果是这样的:[img=http://social.msdn.microsoft.com/Forums/getfile/81565][/img]在xp系统或者win2003里面就会只有一个会话,但是,我在循环里面加上sleep,会导致共享的会话处于空闲状态(如下,会话会间断地出现空闲5秒的情况)main方法:while(true){ShareAccess2share=newShareAccess2("192.168.1.189","XPMUser","123456");share.doworkinshare(@"\192.168.1.189dataMachine70");Thread.Sleep(5000);}
能不能再帮忙看一下如何做啊,我的目的是用完了立即释放掉,尽量少占用共享的连接数,如果代码不好搞的话系统可不可以设置成一旦连接处于空闲状态,立即给他断开?