Discussion:
LogonUser doesn’t work
(too old to reply)
BMW
2007-07-23 16:38:01 UTC
Permalink
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.
BMW
2007-07-23 17:02:04 UTC
Permalink
Hi,

I found out my problem is the wrong password. However, after I corrected the
password, I have connection error as follows:

Cannot connect to the configuration database. For tips on troubleshooting
this error, search for article 823287 in the Microsoft Knowledge Base at
http://support.microsoft.com.

My SQL server is on another machine and the project in VS2005 is on my
machine with sharepoint server and web server.

Please guide me to slove the problem.
Post by BMW
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.
LogonUser failed with error code: 1326
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.
Loading...