Tag: 模拟

在模拟时调用异步WCF服务

我在服务器上运行WCF服务,该服务器配置为接受Kerberos身份validation。 Kerberos工作正常,因此WCF服务知道哪个用户连接到他。 该服务提供一切作为异步方法。 就像这里一样(只是一个清晰的例子)。 public ExampleService : IExampleService { public Task GetUsernameAsync() { return await Task.Run(() => System.Threading.Thread.CurrentPrincipal.Name); } } 在客户端,我有一个Controller(它是一个MVC页面,但这没关系),它调用方法异步。 public ExampleController { public async Task Index() { using(var serviceClient = ServiceFactory.GetServiceClient()) using(Security.Impersonation.Impersonate()) { var data = await serviceClient.GetUsernameAsync(); return View(data); } } } 只要我不使用等待,模仿就可以正常工作。 由于Task不会模拟模拟身份,因此我想知道是否有可能更改Task的执行用户或执行任何其他操作以使模拟在此用例中起作用。 我尝试了一个自定义的awaiter(因为它可以在那个案例中使用Culture),但这根本不起作用(好吧它也不会冒充)。

如何在.NET 1.1中为Impersonation将用户凭据从一个进程传递到另一个进程?

我有一个在特定用户帐户下运行的Windows服务(用.NET 1.1编写)和在多个服务器上运行的服务实例。 我想将用户凭据(用户名,密码,域)从WinForms应用程序传递给服务,并让服务器的本地文件系统中的服务读/写文件模拟传入的凭据。 是否更好地传递用户名,域和密码并让Windows服务执行模拟? 我没有看到如何序列化WindowsIdentity并传​​递一个作为参数,让服务然后执行I / O周围的Impersonate()和Undo()。 作为容器对象,System.Net.NetworkCredential未标记为可序列化,因此传递三个单独的参数似乎是合乎逻辑的。 我基本上使用KB306158中的Impersonation例程。

如何在c#中的同一网络中的另一台机器上保存文件?

我必须将上传的文档保存到另一台本地计算机上。 但我不清楚如何在我的代码中使用模拟。 我使用了以下代码。 protected void btnSave_Click(object sender, EventArgs e) { int result = 0,result1=0; string attachmentpath = “C:\\Documents and Settings\\pr84528\\My Documents\\PRABU\\Tech\\Dot Net\\DocumentManager\\Attachments\\”; ………………………………………………………………………… ………………………………………………………………………… if(fuDocument.HasFile) fuDocument.SaveAs(attachmentpath + fuDocument.FileName); ………………………………………………………………………… ………………………………………………………………………… }

“项目不存在”错误读取SharePoint 2010列表

我在SharePoint 2010中有一个列表。如果我以编程方式(通过自定义webpart)将项目添加到列表中,我可以稍后阅读这些项目并在其他Web部件中显示它们。 但是,如果我尝试读取通过Web界面添加的列表项,我的webpart中会出现以下错误: 物品不存在。 您选择的页面包含不存在的项目。 它可能已被其他用户删除.x81020016 奇怪的是,在调试器中,我看到列表项已正确读取。 我把头发上留下的东西拉过来。 有任何想法吗? 以下是任何关心的人的答案: 我这样调用我的页面 – mywebpage.aspx?id = 1,其中id = 1是我希望我的webpart显示的列表中项目的ID。 由于某些只有Microsoft知道的原因,在查询字符串中使用’id’是禁止的。 所以我将param名称更改为’lid’,现在一切都像我期望的那样工作。 感谢大家的回复。

如何从C#运行PowerShell脚本

我正在尝试使用C#运行PowerShell脚本,但我没有任何成功。 这是我的function: private void ExecutePowerShellCommand(string scriptfile) { RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(); scriptInvoker.Invoke(“Set-ExecutionPolicy Unrestricted”); Pipeline pipeline = runspace.CreatePipeline(); //Here’s how you add a new script with arguments Command myCommand = new Command(scriptfile); //CommandParameter testParam = new CommandParameter(“key”, “value”); //myCommand.Parameters.Add(testParam); pipeline.Commands.Add(myCommand); // Execute PowerShell script pipeline.Invoke(); } […]

将文件复制到网络共享我无法访问

这是这个问题的延伸 我正在尝试将文件从本地用户临时文件夹复制到远程文件共享。 我无权访问远程文件共享,因此我必须冒充用户。 现在,我可以从远程服务器成功读取文件并在本地复制它,但是我无法将本地文件写入共享,因为它给了我错误: LOCAL文件的访问被拒绝(因为我现在正在模仿另一个用户)。 如果你需要一些代码,我可以发布它。

WCF并行模拟

我有一个带有“ImpersonationOption.Required”的WCF服务。 使用并行性时,模拟似乎没有流过。 例如: Parallel.ForEach(items => results.Add(SystemUtil.WindowsUser.Name) 将使用模拟用户返回一个数字,并使用app pool用户返回一个数字。 是否可以假冒并行工作? 最好, 渣 更新: 这是IIS服务端的实际代码。 [OperationBehavior(Impersonation = ImpersonationOption.Required)] public string[] WhoAmI(int numOfTests) { var tests = new List(); for (var i = 0; i < numOfTests; i++) tests.Add(i); var results = new ConcurrentBag(); Parallel.ForEach(tests, (test) => results.Add(WindowsIdentity.GetCurrent(false).Name)); return results.ToArray(); } 如果我传入numOfTests = 10,它会生成10个任务并返回每个任务的WindowsIndentity名称。 我得到的是~70%“IIS APPPOOL.NET v4.0”和~30%我。 如何设置它以使我的身份始终进入Parallel.ForEach? […]

获取默认凭据?

我的域名中有A.aspx页面 此页面(在其c#代码中)向另一个页面发出请求。( B.aspx )。 – 这也是我的域名 整个站点都在Windows身份validation中 HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(“http://mydom.com/b.aspx”); loHttp.UseDefaultCredentials = true; loHttp.Timeout = 100000; HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse(); Encoding enc = Encoding.GetEncoding(“UTF-8”); // Windows default Code Page StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc); string lcHtml = loResponseStream.ReadToEnd(); loWebResponse.Close(); loResponseStream.Close(); return lcHtml; 我在我的网站上使用模拟到特定帐户。 该帐户正在通过以下声明转移: loHttp.UseDefaultCredentials = true; 一切都很好….. 但是,我希望看到这些凭据(我需要他们的“获取”) 我知道当前的线程帐户(受模仿影响)由下式给出: WindowsIdentity.GetCurrent().Name 但我希望看到UseDefaultCredentials中的值! 就像是 […]

C#模拟VolumeMute按

我得到以下代码来模拟volumemute按键: [DllImport(“coredll.dll”, SetLastError = true)] static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); byte VK_VOLUME_MUTE = 0xAD; const int KEYEVENTF_KEYUP = 0x2; const int KEYEVENTF_KEYDOWN = 0x0; private void button1_Click(object sender, EventArgs e) { keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_KEYDOWN, 0); keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_KEYUP, 0); } 这段代码不起作用。 我知道有另一种方法可以通过SendMessageW静音/取消静音,但我不想使用SendMessageW,因为我使用KeyState来检测我需要静音还是取消静音(如果用户想要取消静音并且已经取消静音然后我不需要切换 – 这就是为什么我需要模拟VolumeMute按键) 谢谢。

使用适配器模式包装系统对象(File,ServiceController等)与unit testing绕行有什么好处?

请考虑以下停止服务的方法: Public Function StopService(ByVal serviceName As String, ByVal timeoutMilliseconds As Double) As Boolean Try Dim service As New ServiceController(serviceName) Dim timeout As TimeSpan = TimeSpan.FromMilliseconds(timeoutMilliseconds) service.[Stop]() If timeoutMilliseconds <= 0 Then service.WaitForStatus(ServiceControllerStatus.Stopped) Else service.WaitForStatus(ServiceControllerStatus.Stopped, timeout) End If Return service.Status = ServiceControllerStatus.Stopped Catch ex As Win32Exception 'error occured when accessing a system API' Return False Catch […]