[问题] 使用不同使用者帐户来执行程式

楼主: jayzhuang (Jay)   2019-12-20 11:01:30
各位大大好,在下目前遇到一个奇怪的情况
系统:Win10
工具:VS2019
因为公司需要做的专案,我已经找到原因(权限问题)
但公司说希望我研究Runas的方式,让我们直接跟厂商要到一个管理者权限帐号密码之后
由我们的系统内直接写好帐号密码,以不需要去更动客户端的设定来执行我们系统。
我第一次做这些工具,我先从几个方向来执行。
先能让一些系统程式能以不同的身分执行,所以我先做了个简易的WinForm。
我的环境是一台AD Server,搭配一台Win7。(系统需求一定要AD网域的环境)
主要的需求更动:能让程式能以不同使用者来执行,如下图:
我用Runas执行某使用者来执行notepad.exe,并透过工作管理员确认
输入了4个使用者;
Win702、Win70202,是Win7本机的使用者
(Win702是本机管理者,Win70202一般使用者)
另外两个:
[email protected]
[email protected]
这两个都是AD Server的帐户。
https://imgur.com/nf64XBS
因为我要确认我设定的AD环境没有问题,透过cmd执行不同使用者,确定可以。
然后我要先写一个小程式要来厘清使用者帐户输入的问题
(密码我都是设定1~9,只有帐号名称不同。)
透过这个Winform,输入使用者名称,按下按钮来执行。
程式码内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//新增
using System.Diagnostics;
using System.Security;
using System.ComponentModel;
namespace TestRunAs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private SecureString ConvertStringToSecureString(string pwd)
{
SecureString password = new SecureString();
foreach (char c in pwd)
{
password.AppendChar(c);
}
return password;
}
private void Button1_Click(object sender, EventArgs e)
{
//要执行的档案名称
string fileName = "notepad.exe";
// 指定要执行程式的使用者名称
string userName = txb_username.Text;
//[email protected]
// 指定要执行程式的使用者名称密码,但需要是 SecureString 类别
SecureString password = ConvertStringToSecureString("123456789");
try
{
// 执行程式
Process.Start(fileName, userName, password, null);
}
catch (Win32Exception win32Exception)
{
// 如果使用者名称或密码不正确时会丢出 Win32Exception
MessageBox.Show(win32Exception.Message.ToString());
}
}
}
}
然后我WinForm设计与执行:
我输入Win702确定可以
https://imgur.com/J0RSPKA
但我输入Win70202这个帐户都会这样:
https://imgur.com/3AJtGlf
其他两个帐户也是这样:
https://imgur.com/Cx58NZW
请问是什么原因压? 是我帐户输入方式错误吗? 我曾改曾这样输入:
https://imgur.com/oYOWoIV
但还是不行.....
希望有大神可以为我解答~! m( >< ) m
ps:再一个状况,为什么我WinForm不能够用具有管理者权限的AD帐户执行
反而本机帐户执行却可以压[email protected]@
如下图:
我这只程式的路径与名称
C:\Users\Win702\Desktop\testrunas\Debug\TestRunAs.exe
https://imgur.com/WY9giDf
换另一个帐户执行却失败....
https://imgur.com/vibk70a
作者: kyrc (橘子)   2019-12-20 17:25:00
AD 帐号有 TestRunAs.exe 存取权限?https://i.imgur.com/hEtcNWN.jpg
作者: rexhuang (BlueCancer)   2019-12-21 09:51:00
程式不要放在Win702的桌面,放到D槽或C槽开个新资料夹自己的桌面其他使用者不会有权限读和执行,要手动加权限
作者: cancelpc (阿吉)   2019-12-21 16:22:00
有使用UI的程式很多情况下会需要在该电脑登入过,在那台电脑起始过该帐号一些profile后才能动作Win默认是绑UI、以及帐号。你用要 runas 那个先在那台用人工登入 xyz 过,产生必要profile档后。之后用 runasxyz 就可能可以。至于不想再每台电脑用人工绍登入过一次产生需要的 profile 档,问G吧

Links booklink

Contact Us: admin [ a t ] ucptt.com