在Choral主页中找到这个方法,(没有试验,准备试验中...)
http://blog.joycode.com/choral/archive/2007/05/14/102528.aspx
没有想到那么简单, 以前我还是自己用API函数来搞定, 真是弱智呀.不过把那个用API函数搞定的程序的也贴进来,大家共享一下:
// 以下用户管理用户来登录
protected static WindowsIdentity CreateIdentity( string User, string
Domain, string Password)
{
// The Windows NT user token.
IntPtr tokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3;
tokenHandle = IntPtr.Zero;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(User, Domain, Password,
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
throw new Exception("LogonUser failed with error code: " + ret);
}
System.Diagnostics.Debug.WriteLine("Created user token: " +
tokenHandle);
//The WindowsIdentity class makes a new copy of the token.
//It also handles calling CloseHandle for the copy.
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
[DllImport( " advapi32.dll " , SetLastError = true )]
private static extern bool LogonUser(String lpszUsername, String
lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport( " kernel32.dll " , CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
使用的时候, 应该这样使用:
WindowsImpersonationContext wic
=
CreateIdentity (
"
用户
"
,
"
域名
"
,
"
密码
"
) .Impersonate();
使用完毕,应该释放权限:
wic.Undo ();
结束! 今天真高兴, 终于找到一个捷径,但我提供的程序代码,不仅可以用在MOSS和SharePoint中,开发ASPX、DLL、其它程序也都可以用哦。