运行 成功 Windows 身份验证后的 Excel 文件
Running an Excel File following Successful Windows Authentication
我正在尝试整理一些代码,要求用户输入他们当前的 windows 用户名和密码,以便 运行 一个 excel 文件。但是,我在使用代码的 process.start 部分时遇到问题。不知道我错过了什么。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("advapi32.dll")]
public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
bool issuccess = false;
string username = GetloggedinUserName();
if (username.ToLowerInvariant().Contains(txtUserName.Text.Trim().ToLowerInvariant()) && username.ToLowerInvariant().Contains(txtDomain.Text.Trim().ToLowerInvariant()))
{
issuccess = IsValidateCredentials(txtUserName.Text.Trim(), txtPwd.Text.Trim(), txtDomain.Text.Trim());
}
if (issuccess)
MessageBox.Show("Successfuly Login !!!");
// Here is where I am having trouble. I cannot get this file to run.
Process.Start("Z:\FolderA\Test.xlsx");
else
MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
}
private string GetloggedinUserName()
{
System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
return currentUser.Name;
}
private bool IsValidateCredentials(string userName, string password, string domain)
{
IntPtr tokenHandler = IntPtr.Zero;
bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
return isValid;
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
- Visual Studio 错误信息:
Severity Code Description
Error CS0103 The name 'Process' does not exist in the current context TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs
Error CS1513 } expected TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs
您的 IF
语句中缺少大括号:
if (issuccess)
{
MessageBox.Show("Successfuly Login !!!");
Process.Start("Z:\FolderA\Test.xlsx");
}
else
{
MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
}
在 multi-line IF 语句中始终使用大括号是安全的规则。
我正在尝试整理一些代码,要求用户输入他们当前的 windows 用户名和密码,以便 运行 一个 excel 文件。但是,我在使用代码的 process.start 部分时遇到问题。不知道我错过了什么。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("advapi32.dll")]
public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
bool issuccess = false;
string username = GetloggedinUserName();
if (username.ToLowerInvariant().Contains(txtUserName.Text.Trim().ToLowerInvariant()) && username.ToLowerInvariant().Contains(txtDomain.Text.Trim().ToLowerInvariant()))
{
issuccess = IsValidateCredentials(txtUserName.Text.Trim(), txtPwd.Text.Trim(), txtDomain.Text.Trim());
}
if (issuccess)
MessageBox.Show("Successfuly Login !!!");
// Here is where I am having trouble. I cannot get this file to run.
Process.Start("Z:\FolderA\Test.xlsx");
else
MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
}
private string GetloggedinUserName()
{
System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
return currentUser.Name;
}
private bool IsValidateCredentials(string userName, string password, string domain)
{
IntPtr tokenHandler = IntPtr.Zero;
bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
return isValid;
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
- Visual Studio 错误信息:
Severity Code Description
Error CS0103 The name 'Process' does not exist in the current context TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs
Error CS1513 } expected TestApp C:\C#\Windows Authentication\C#\TestApp\Form1.cs
您的 IF
语句中缺少大括号:
if (issuccess)
{
MessageBox.Show("Successfuly Login !!!");
Process.Start("Z:\FolderA\Test.xlsx");
}
else
{
MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
}
在 multi-line IF 语句中始终使用大括号是安全的规则。