[英]Access denied when using powershell on IIS webserver
我的 0ffice 365 用戶有一個密碼重置網站。 它在后台運行 powershell 命令來重置用戶密碼。 出於某種原因,當我使用 VS 並且運行帶有頁面的瀏覽器時,它工作正常。 但是,當我從實時站點運行它時,出現此錯誤
Processing data from remote server failed with the following error message: Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
該頁面是帶有 C# 代碼的 aspx.net。
我曾嘗試使用 impersonator 類,但這也不起作用。
這是我的一些代碼。
using (new Impersonator("user", "domain", "pass"))
{
PSCredential creds = new PSCredential("office365email", "password");
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
new Uri("https://server.outlook.com/PowerShell-LiveID?PSVersion=2.0"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", creds);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
try
{
runspace.Open();
pipeline = runspace.CreatePipeline();
Command forwardcommand = new Command("Set-Mailbox");
forwardcommand.Parameters.Add("Identity", user);
forwardcommand.Parameters.Add("Password", pass);
pipeline.Commands.Add(forwardcommand);
try
{
pipeline.Invoke();
runspace.Close();
pipeline.Stop();
return "Password Successfully Changed";
}
catch (Exception er)
{
runspace.Close();
pipeline.Stop();
return er.Message;
}
}
catch (PSRemotingTransportException eer)
{
runspace.Close();
return eer.Message;// "Server busy please try again later";
}
}
任何想法為什么我不能從實際網站上做到這一點,但我可以在我的本地主機上做到這一點?
<authentication mode="Windows"/>
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
<providers>
<clear/>
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider"/>
</providers>
</roleManager>
<identity impersonate="true"/>
在 IIS 中,我將 ApplicationPoolIdentity 更改為 LocalService。 這解決了運行已發布的 Visual Studio 網站時訪問被拒絕錯誤,但它在發布前運行良好。
這可能是一個“雙跳”身份/安全問題。
在 localhost 上運行時,Windows 身份在同一台機器上是已知的。
在 Web 服務器上運行時,至少還涉及一台機器。
相關問題包括: IIS 使用什么身份來運行網頁? 該 ID 是否有權使用網絡? 該 ID 是否有權在遠程服務器上執行密碼重置?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.