简体   繁体   English

在 IIS 网络服务器上使用 powershell 时访问被拒绝

[英]Access denied when using powershell on IIS webserver

I have a password reset website for my 0ffice 365 users.我的 0ffice 365 用户有一个密码重置网站。 It runs powershell commands in the background to reset user passwords.它在后台运行 powershell 命令来重置用户密码。 For some reason when I am using VS and I run the browser with the page it works fine.出于某种原因,当我使用 VS 并且运行带有页面的浏览器时,它工作正常。 However when I run it from the live site I get this error但是,当我从实时站点运行它时,出现此错误

Processing data from remote server failed with the following error message: Access is denied. For more information, see the about_Remote_Troubleshooting Help topic. 

The page is aspx.net with C# code behind.该页面是带有 C# 代码的 aspx.net。

I have tried using the impersonator class but that does not work either.我曾尝试使用 impersonator 类,但这也不起作用。

Here is some of my code.这是我的一些代码。

 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";
        }
    }

Any ideas why I can't do it from the actual website but I can on my local host?任何想法为什么我不能从实际网站上做到这一点,但我可以在我的本地主机上做到这一点?

<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"/>

In IIS I changed the ApplicationPoolIdentity to LocalService.在 IIS 中,我将 ApplicationPoolIdentity 更改为 LocalService。 This resolved the Access Denied error when running a published Visual Studio web site when it worked fine before publishing.这解决了运行已发布的 Visual Studio 网站时访问被拒绝错误,但它在发布前运行良好。

This could be a "double-hop" identity / security problem.这可能是一个“双跳”身份/安全问题。

When running on localhost, the Windows identity is known on the same machine.在 localhost 上运行时,Windows 身份在同一台机器上是已知的。
When running on the web server, there is at least one more machine involved.在 Web 服务器上运行时,至少还涉及一台机器。

Relevant questions include: What identity is IIS using to run the web page?相关问题包括: IIS 使用什么身份来运行网页? Does that ID have permissions to use networking?该 ID 是否有权使用网络? Does that ID have permissions to execute password-reset on the remote server?该 ID 是否有权在远程服务器上执行密码重置?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM