C# 错误:试图读取或写入受保护的内存这通常表示其他内存已损坏
我在下面的程序中得到了在标题中声明的错误。 我正在尝试模拟用户。 这是我的代码库C# 错误:试图读取或写入受保护的内存这通常表示其他内存已损坏,c#,ssas,C#,Ssas,我在下面的程序中得到了在标题中声明的错误。 我正在尝试模拟用户。 这是我的代码库 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data.OleDb; using Microsoft.AnalysisServices.AdomdClient; using Microsoft.Analys
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using Microsoft.AnalysisServices.AdomdClient;
using Microsoft.AnalysisServices;
using Microsoft.AnalysisServices.Xmla;
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace cubeStructure
{
class Program
{
static void Main(string[] args)
{
connectToServer();
}
private static void connectToServer()
{
try
{
Console.WriteLine("Connecting to server...");
XmlaClient oConn = new XmlaClient();
Boolean validUser = false;
validUser = impersonateValidUser("haadmin", "HAICPM", "ha05admin");
if (validUser)
{
Console.WriteLine("User Aethunticated");
}
else
{
Console.WriteLine("Authentication failed");
}
string connectionString = "Provider=MSOLAP.3;Data Source=192.168.136.233\\sql2008;Persist Security Info=True;User ID=sa;Initial Catalog=AdventureWorksDW";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Boolean RevertToSelf();
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern long CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int LogonUserA(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, IntPtr phToken);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int DuplicateToken(IntPtr ExistingTokenHandle, int ImpersonationLevel, IntPtr DuplicateTokenHandle);
public static WindowsImpersonationContext impersonationContext;
public static bool impersonateValidUser(string userName, string domain, string password)
{
Console.WriteLine("Going Step 1");
try
{
bool functionReturnValue = false;
int LOGON32_LOGON_INTERACTIVE = 2;
int LOGON32_PROVIDER_DEFAULT = 0;
WindowsIdentity tempWindowsIdentity = default(WindowsIdentity);
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
functionReturnValue = false;
Console.WriteLine("Going Step 2");
if (RevertToSelf())
{
if (LogonUserA("haadmin", ".", "ha05admin", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) != 0)
{
if (DuplicateToken(token, 2, tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if ((impersonationContext != null))
{
functionReturnValue = true;
}
}
}
}
if (!tokenDuplicate.Equals(IntPtr.Zero))
{
CloseHandle(tokenDuplicate);
}
if (!token.Equals(IntPtr.Zero))
{
CloseHandle(token);
}
return functionReturnValue;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
}
}
我在logonUserA方法中遇到异常。
将DLL添加到代码中时是否有任何错误?
有谁能在这方面帮助我吗
谢谢请让您的问题更加具体,首先从代码中的何处出现此异常开始?pinvoke声明中的错误太多了。将LogonUserA与CharSet.Auto一起使用就是其中之一。请访问pinvoke.net以找到好的声明。@HansPassant在pinvoke.net上有很多写得很差的p/Invoke声明。这个网站真的很有用,但我不会太依赖它。@All。。谢谢你的帮助。我在这里犯的错误是需要给出logonusera方法的引用,如logonusera字符串lpszUsername、字符串lpszDomain、字符串lpszPassword、int dwLogonType、int dwLogonProvider、ref IntPtr phToken。。。同样的道理也适用于DuplicateToken方法。。。