BMW
2007-07-23 16:38:01 UTC
Hi
I am creating a website service for copying a file from share point library
to local compute. I created a class which is the same as the Using
Impersonation to Access Data that the Current User Cannot Access at
http://msdn2.microsoft.com/en-us/library/aa505327.aspx#wsstipstricks_usingimpersonationtoaccessdatathatthecurrentusercannotaccess.
The identity is used is Site collection administrator for impersonation.
However, it seems that the LogonUser doesn’t work. I got the error like that:
LogonUser failed with error code: 1326
There is the code for impersonation:
class SecurityHelpers
{
private SecurityHelpers() {}
[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);
public static WindowsIdentity CreateIdentity(
string userName, string domain, string password)
{
IntPtr tokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK_CLEARTEXT = 3;
tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domain, password,
LOGON32_LOGON_NETWORK_CLEARTEXT,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
throw new Exception("LogonUser failed with error code: " + ret);
}
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
}
There is the code I use the above class and connect the Sharepoint
wic = SecurityHelpers.CreateIdentity("user", "Domain.ca",
"psd").Impersonate();
// get the library path
String docLibPath = "http://m2:10001/";
//first, get the site containing the library
SPSite site = new SPSite(docLibPath);
SPWeb web = site.RootWeb;
SPFolder docFolder = web.Folders["Folder1"];
Would you tell me how to fix this problem?
Thanks.
I am creating a website service for copying a file from share point library
to local compute. I created a class which is the same as the Using
Impersonation to Access Data that the Current User Cannot Access at
http://msdn2.microsoft.com/en-us/library/aa505327.aspx#wsstipstricks_usingimpersonationtoaccessdatathatthecurrentusercannotaccess.
The identity is used is Site collection administrator for impersonation.
However, it seems that the LogonUser doesn’t work. I got the error like that:
LogonUser failed with error code: 1326
There is the code for impersonation:
class SecurityHelpers
{
private SecurityHelpers() {}
[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);
public static WindowsIdentity CreateIdentity(
string userName, string domain, string password)
{
IntPtr tokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK_CLEARTEXT = 3;
tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domain, password,
LOGON32_LOGON_NETWORK_CLEARTEXT,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
throw new Exception("LogonUser failed with error code: " + ret);
}
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
}
There is the code I use the above class and connect the Sharepoint
wic = SecurityHelpers.CreateIdentity("user", "Domain.ca",
"psd").Impersonate();
// get the library path
String docLibPath = "http://m2:10001/";
//first, get the site containing the library
SPSite site = new SPSite(docLibPath);
SPWeb web = site.RootWeb;
SPFolder docFolder = web.Folders["Folder1"];
Would you tell me how to fix this problem?
Thanks.