在ASP.NET/c#中启动服务,具有权限
-
07-09-2020 - |
题
ServiceController svcController = new ServiceController("InvidualFileConversion");
if (svcController != null)
{
try
{
svcController.Stop();
svcController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
svcController.Start();
}
catch (Exception ex)
{
// error
}
}
.
现在,当我运行此时,我收到错误“无法打开计算机上的InvidualFileConversion服务”,附加消息:“Acces被拒绝”。
我知道这是一个有权限的问题,但如何为自己提供适当的权限? 不要带有我应该写的答案:因为我已经尝试过它,它没有用。此外,我认为当我只需要这几行代码时,我认为这不是一个真正的方式为整个网站设置这一点。
编辑:我在代码中添加了它,它仍然不起作用,我在同一个地方获得相同的例外。现在它看起来像这样:
protected void ConvertLink_OnClick(object sender, EventArgs e)
{
//convert();
try
{
//--need to impersonate with the user having appropriate rights to start the service
Impersonate objImpersonate = new Impersonate(domainName, userName, userPassword);
if (objImpersonate.impersonateValidUser())
{
//--write code to start/stop the window service
startWindowsService();
objImpersonate.undoImpersonation();
}
}
catch (Exception Ex)
{ Response.Write(Ex.Message + Ex.InnerException.Message); }
}
private void startWindowsService()
{
ServiceController svcController = new ServiceController("InvidualFileConversion");
if (svcController != null)
{
try
{
svcController.Stop();
svcController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
svcController.Start();
}
catch (Exception ex)
{
// error
}
}
}
.
我有一个模特类的类,如下所示:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Security.Principal;
using System.Runtime.InteropServices;
/// <summary>
/// Summary description for Impersonate
/// </summary>
public class Impersonate
{
#region "Class Members"
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
#endregion
#region "Class Properties"
public string domainName { get; set; }
public string userName { get; set; }
public string userPassword { get; set; }
#endregion
public Impersonate()
{
//
// TODO: Add constructor logic here
//
}
public Impersonate(string domainName, string userName, string userPassword)
{
this.domainName = domainName;
this.userName = userName;
this.userPassword = userPassword;
}
#region "Impersonation Code"
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
public bool impersonateValidUser()
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(this.userName, this.domainName, this.userPassword, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
public void undoImpersonation()
{
impersonationContext.Undo();
}
#endregion
}
.
所以验证的事情是有效但不能解决我的问题..我想权限仍然存在问题..我该怎么办才能改变这个?
编辑2:我所接受的下一步涉及:
- 创建一个应用程序池,其中标识设置为用户(这是管理员组的成员)。
- 将服务“登录”设置为同一用户。
- 再次运行Web应用程序后,它仍然失败.. 但是
如果我在代码中放入管理员帐户作为凭据,它可以工作..(即使我没有在应用程序池中使用管理员和服务......)<)< / p>
换句话说,我可以通过管理员帐户获得我想要的东西,但不是我创建自己的帐户并具有管理权限。我仍然希望与我自己的用户做出这项工作,因为我认为将管理员凭据提供的管理员凭据不那么安全。
在sidenote上,在我上工作的服务器上,我有一个具有管理员权限的帐户,但不是'管理员的帐户。
编辑3:这是奇怪的。我似乎现在工作但是: - 没有模拟方法(它不起作用)。 - 我只是做了罗布所说的。我使用我定义的用户自己的应用程序池。 Windows服务也有该用户也指定。 (用户接收到服务右侧登录) - 有了这个,似乎工作。 - 但如果通过我的网站调试,我仍然会被否认。但是,如果只是通过浏览器和它的IP访问我的网站,我可以启动服务。
总结: - 模拟方法不起作用。 - 只要使用自动创建的应用程序池,如果该服务也指定了该服务,则使用Current用户使用。但它在调试模式下不起作用(仍然在那里拒绝)。
这篇文章很大,我想知道是否有任何仍然读过它......但也许有人可以向我提供任何细节?我担心将来会在未来的某个地方失败。
任何评论都将得到理解! Floris
解决方案
您必须确保您的网站在有足够右开始服务的用户下运行。 另请确保在IIS上禁用匿名身份验证 一种很好的方法可以是创建一个有权在服务器Web上启动服务权的用户,然后创建一个在该user下运行的应用程序池。然后您必须让您的网站使用该应用程序池。使用此方法所有网站将在您只需创建的用户下运行。 如果您想要更粒度,您仍然可以创建具有启动服务权限但是为应用程序池使用它的用户,而您只能使用模拟,只能使用模拟器在该用户下启动服务运行的页面。您只能为此方法使用无摩擦功能!
有关详细信息,请查看以下链接: