使用本地C#应用程序检查Windows中的MaxPasswordAge
我在互联网上查找这方面的文档时遇到了最糟糕的情况。基本上,我想知道Secpol MaXPWAge设置为90或更小,并将其显示在文本框中(为了方便起见,我们称之为textbox1)。我在auditor中搜索了WMI解决方案、注册表、GPEDIT,但没有找到任何结果。我确实发现了这一点,但老实说,我不知道如何使用相同的代码来检查最大密码期限,而不是复杂性要求。拜托,有人能告诉我我应该在这里做什么吗?C#不是我的主要语言使用本地C#应用程序检查Windows中的MaxPasswordAge,c#,wmi,password-policy,windows-users,C#,Wmi,Password Policy,Windows Users,我在互联网上查找这方面的文档时遇到了最糟糕的情况。基本上,我想知道Secpol MaXPWAge设置为90或更小,并将其显示在文本框中(为了方便起见,我们称之为textbox1)。我在auditor中搜索了WMI解决方案、注册表、GPEDIT,但没有找到任何结果。我确实发现了这一点,但老实说,我不知道如何使用相同的代码来检查最大密码期限,而不是复杂性要求。拜托,有人能告诉我我应该在这里做什么吗?C#不是我的主要语言 使用系统; 使用System.Collections.Generic; 使用系
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
班级计划
{
静态void Main(字符串[]参数)
{
Write(PasswordComplexityPolicy());
}
静态布尔密码复杂策略()
{
var tempFile=Path.GetTempFileName();
过程p=新过程();
p、 StartInfo.FileName=Environment.ExpandEnvironmentVariables(@“%SystemRoot%\system32\secedit.exe”);
p、 StartInfo.Arguments=String.Format(@/export/cfg”“{0}”“/quiet”,tempFile);
p、 StartInfo.CreateNoWindow=true;
p、 StartInfo.UseShellExecute=false;
p、 Start();
p、 WaitForExit();
var file=IniFile.Load(tempFile);
IniSection systemAccess=null;
var passwordComplexityString=“”;
var-passwordComplexity=0;
返回文件.Sections.TryGetValue(“系统访问”,out-systemAccess)
&&systemAccess.TryGetValue(“PasswordComplexity”,out passwordComplexityString)
&&Int32.TryParse(passwordComplexityString,out passwordComplexity)
&&密码复杂度==1;
}
类文件
{
公共静态文件加载(字符串文件名)
{
var result=新文件();
result.Sections=newdictionary();
var section=新的IniSection(String.Empty);
结果.Sections.Add(section.Name,section);
foreach(文件中的var行。ReadAllLines(文件名))
{
var trimedLine=line.Trim();
开关(第[0]行)
{
格“;”:
继续;
案例“[”:
section=新的IniSection(trimedLine.Substring(1,trimedLine.Length-2));
结果.Sections.Add(section.Name,section);
打破
违约:
var parts=trimedLine.Split('=');
如果(零件长度>1)
{
添加(零件[0].Trim(),零件[1].Trim());
}
打破
}
}
返回结果;
}
公共IDictionary节{get;private set;}
}
类别:字典
{
公共部分(字符串名称):基(StringComparer.OrdinalIgnoreCase)
{
this.Name=Name;
}
公共字符串名称{get;private set;}
}
}
我会像这样编写ini文件
类:
class IniFile : Dictionary<string,Dictionary<string,string>> {
public IniFile(string filename) {
var currentSection = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Add("", currentSection);
foreach (var line in File.ReadAllLines(filename)) {
var trimedLine = line.Trim();
switch (line[0]) {
case ';':
continue;
case '[':
currentSection = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Add(trimedLine.Substring(1, trimedLine.Length - 2), currentSection);
break;
default:
var parts = trimedLine.Split('=');
if (parts.Length > 1) {
currentSection.Add(parts[0].Trim(), parts[1].Trim());
}
break;
}
}
}
public string this[string sectionName, string key] {
get {
Dictionary<string, string> section;
if (!TryGetValue(sectionName, out section)) { return null; }
string value;
if (!section.TryGetValue(key, out value)) { return null; }
return value;
}
}
public int? GetInt(string sectionName, string key) {
string stringValue = this[sectionName, key];
int result;
if (!int.TryParse(stringValue, out result)) { return null; }
return result;
}
}
static void Main(string[] args) {
//This will be the path of the temporary file which contains the output of secedit.exe
string tempFile;
//Write the output of secedit.exe to the temporary file
GenerateSecEditOutput(out tempFile);
//Parse the temporary file
var iniFile = new IniFile(tempFile);
//Read the maximum password age from the "System Access" section
var maxPasswordAge = iniFile.GetInt("System Access", "MaximumPasswordAge");
if (maxPasswordAge.HasValue) {
Console.WriteLine("MaxPasswordAge = {0}", maxPasswordAge);
} else {
Console.WriteLine("Unable to find MaximumPasswordAge");
}
Console.ReadKey(true);
}
然后,Main
方法如下所示:
class IniFile : Dictionary<string,Dictionary<string,string>> {
public IniFile(string filename) {
var currentSection = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Add("", currentSection);
foreach (var line in File.ReadAllLines(filename)) {
var trimedLine = line.Trim();
switch (line[0]) {
case ';':
continue;
case '[':
currentSection = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Add(trimedLine.Substring(1, trimedLine.Length - 2), currentSection);
break;
default:
var parts = trimedLine.Split('=');
if (parts.Length > 1) {
currentSection.Add(parts[0].Trim(), parts[1].Trim());
}
break;
}
}
}
public string this[string sectionName, string key] {
get {
Dictionary<string, string> section;
if (!TryGetValue(sectionName, out section)) { return null; }
string value;
if (!section.TryGetValue(key, out value)) { return null; }
return value;
}
}
public int? GetInt(string sectionName, string key) {
string stringValue = this[sectionName, key];
int result;
if (!int.TryParse(stringValue, out result)) { return null; }
return result;
}
}
static void Main(string[] args) {
//This will be the path of the temporary file which contains the output of secedit.exe
string tempFile;
//Write the output of secedit.exe to the temporary file
GenerateSecEditOutput(out tempFile);
//Parse the temporary file
var iniFile = new IniFile(tempFile);
//Read the maximum password age from the "System Access" section
var maxPasswordAge = iniFile.GetInt("System Access", "MaximumPasswordAge");
if (maxPasswordAge.HasValue) {
Console.WriteLine("MaxPasswordAge = {0}", maxPasswordAge);
} else {
Console.WriteLine("Unable to find MaximumPasswordAge");
}
Console.ReadKey(true);
}
如果要将值放入某个文本框中,步骤大致相同。我们可以避免整数解析,并使用IniFile
的索引器:
string tempFile;
GenerateSecEditOutput(out tempFile);
var iniFile = new IniFile(tempFile);
//assuming tb is a variable referring to a textbox
tb.Text = iniFile["System Access", "MaximumPasswordAge"];
请记住,
secedit.exe
需要管理员权限才能运行。如果没有管理员权限,代码不会失败;临时文件将是空的。有关如何执行此操作的建议,请参阅。这是一种欺骗,但如果您只寻找一件事,它会起作用。基本上,它会启动一个新进程并运行最大密码期限
字段。请尝试,但您可能必须以管理员身份运行它:
var process = new Process
{
StartInfo = new ProcessStartInfo()
{
FileName = "net",
Arguments = "accounts",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
string text = "";
while (!process.StandardOutput.EndOfStream)
{
text = process.StandardOutput.ReadLine();
if (text != null && text.StartsWith("Maximum password age (days):"))
break;
}
if (text == null || !text.StartsWith("Maximum password age (days):"))
return;
text = text.Replace("Maximum password age (days):", "").Trim();
textBox1.Text = text;
var si=new不应该为null吗?它不会按原样编译now@JoePearson修正了。我最初是在脑海中写下的,所以它可能还没有编译。不,这个编译得很好。那么告诉我,我如何在windows窗体上检索数据。例如,将检索到的数据放入文本框。我看到的唯一潜在问题是GenerateSecE编辑输出(临时文件);将不知道Var来自何处/如何处理它是的!这是阻止这部杰作编译的唯一原因不!不需要管理员访问它似乎运行得很好。这很好,谢谢you@JoePearson-
net accounts
也可用于设置最大密码期限。因此,如果需要该功能,请修改删除此代码应该不难。@Icemanmind是否有类似的方法通过LUSRMGR.msc确定用户的密码是否允许过期?@JoePearson-不,我不这么认为。您必须使用更复杂的代码。