Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET/C页面中提示输入凭据,传递到PowerShell_C#_Asp.net_Powershell_Credentials_Networkcredentials - Fatal编程技术网

C# 在ASP.NET/C页面中提示输入凭据,传递到PowerShell

C# 在ASP.NET/C页面中提示输入凭据,传递到PowerShell,c#,asp.net,powershell,credentials,networkcredentials,C#,Asp.net,Powershell,Credentials,Networkcredentials,我希望建立一个概念验证/演示网页,该网页将: 作为服务运行(最初不需要身份验证) 提示输入凭据(即,为了允许提供备用凭据,最好将其捕获为System.Management.Automation.PSCredential)。这里不需要身份验证,我只需要捕获凭据 将凭据传递到PowerShell(例如shell.Commands.AddArgument(pscred)) 在PowerShell中使用这些凭据(作为传入,或转换为System.Management.Automation.PSCrede

我希望建立一个概念验证/演示网页,该网页将:

  • 作为服务运行(最初不需要身份验证)
  • 提示输入凭据(即,为了允许提供备用凭据,最好将其捕获为System.Management.Automation.PSCredential)。这里不需要身份验证,我只需要捕获凭据
  • 将凭据传递到PowerShell(例如shell.Commands.AddArgument(pscred))
  • 在PowerShell中使用这些凭据(作为传入,或转换为System.Management.Automation.PSCredential)
  • 澄清一下:我不想验证凭据。我只是想安全地收集它们。尽管我现在关心的是,这是胡言乱语,但我大概想保护的是胡言乱语
我有一个使用Visual C#的ASP.NET Web应用程序项目(与此类似)。我已启动并运行该页面(即,它使用适当的帐户运行,它运行PowerShell等)-我希望以合理安全的方式提示和存储凭据。我想有一些标准的方法可以做到这一点。还是因为这是一个服务器端应用程序,所以我读得太多了

以下是我迄今为止的代码摘录:

  • Default.aspx(将根据场景进行更改):


    • 我认为最简单的方法是使用模拟。i、 e.您获得该用户的凭据,并作为该用户启动powershell进程,该进程允许您作为该用户执行任何您想要的操作

      见:

      -具有模拟当前用户以运行PowerShell的步骤


      -StackOverflow关于如何使用模拟运行PowerShell的问题。

      鉴于我的新帐户,我似乎无法投票支持您的回答!无论如何,是的,这是我的退路。我正在描绘的用例包括启动网页的标准用户帐户、选择要运行的函数(例如,需要管理权限的函数)以及提示输入管理凭据,这些凭据将传递到脚本中。这将允许标准帐户浏览网站,而不是强制用户以管理员帐户的身份打开浏览器。签出此链接如何:这样,您的常规浏览在一个帐户下,您可以使用另一个帐户启动powershell进程。谢谢,看起来很酷!可能会在一些场景中使用它。如果我有一个ASP.NET前端,我不知道如何安全地获取和存储C#要使用的凭据。不幸的是,web端是我最不熟悉的。似乎有些人把你的问题解释为“推荐”式的问题(要求图书馆或类似的东西来解决你的问题)。我不确定您是在做什么,但我确实认为您可以以更好的方式处理这个问题(使其成为堆栈溢出的“主题”)。我认为,如果你在试图收集/存储/传递所需信息的过程中发布了一些代码,然后描述你在哪里被卡住了,那么这个问题值得重新讨论。
      <asp:TextBox ID="Input" runat="server" TextMode="MultiLine" Width="400px" Height="73px" ></asp:TextBox>  
      <asp:TextBox ID="InputParam" runat="server" TextMode="MultiLine" Width="200px" Height="20px" ></asp:TextBox>  
      <asp:Button ID="ExecuteCode" runat="server" Text="Execute" Width="200" onclick="ExecuteCode_Click" />
      <asp:TextBox ID="ResultBox" TextMode="MultiLine" Width="700" Height="200" runat="server"></asp:TextBox>
      <asp:TextBox ID="ErrorBox" TextMode="MultiLine" Width="700" Height="200" runat="server"></asp:TextBox>
      
      protected void ExecuteCode_Click(object sender, EventArgs e)
      {
          // Clean the Result TextBox
          ResultBox.Text = string.Empty;
      
          // Initialize PowerShell engine
          var shell = PowerShell.Create();
      
          // Add the script and an argument to the PowerShell object
          shell.Commands.AddScript(Input.Text);
          shell.Commands.AddArgument(InputParam.Text);
      
          // Execute the script
          var results = shell.Invoke();
      
          // display results, with BaseObject converted to string
          // Note : use | out-string for console-like output
          if (results.Count > 0)
          {
              // We use a string builder ton create our result text
              var builder = new StringBuilder();
      
              foreach (var psObject in results)
              {
                  // Convert the Base Object to a string and append it to the string builder.
                  // Add \r\n for line breaks
                  builder.Append(psObject.BaseObject.ToString() + "\r\n");
              }
      
              // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
              ResultBox.Text = Server.HtmlEncode(builder.ToString());
          }
      
          //Collect errors
          var PSErrors = shell.Streams.Error.ReadAll();
      
          if (PSErrors != null)
          {
      
              // We use a string builder ton create our error text
              var builder = new StringBuilder();
      
              foreach (var psError in PSErrors)
              {
                  // Convert the exception Object to a string and append it to the string builder.
                  // Add \r\n for line breaks
                  builder.Append(psError.Exception.ToString() + "\r\n");
              }
              // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
              ErrorBox.Text = Server.HtmlEncode(builder.ToString());
          }
      
      }
      
      PSCredential pscred = this.Host.UI.PromptForCredential("Enter username/password","", "", "");
      shell.Commands.AddArgument(pscred);