浅谈.NET(C#)与Windows用户账户信息的获取
原文:浅谈.NET(C#)与Windows用户账户信息的获取
目录
1. 用户账户名称 - 使用Environment类
使用Environment可以返回当前系统环境的一些常用信息,其中包括用户账户名称,则不需要额外使用System.Security.Principal中的类。
//用户名
Console.WriteLine(Environment.UserName);
//计算机NetBIOS名称
Console.WriteLine(Environment.MachineName);
//计算机网络域名称
Console.WriteLine(Environment.UserDomainName);
这是我的电脑的相应信息:
Mgen
MGEN-PC
Mgen-PC
这些信息也可以在计算机属性中查看:
2. 用户账户信息 - 使用WindowsIdentity和IdentityReference
System.Security.Principal.IIdentity接口是用来定义标识对象的基本功能,其派生类WindowsIdentity则直接代表着一个Windows的用户账户。用此类我们可以获取相关用户信息。
同时System.Security.Principal.IdentityReference代表一个标识,其派生类NTAccount和SecurityIdentifier类可以分别代表账户全称和安全标识符
(SID)。不同IdentityReference可以通过Translate方法进行类型转换。
//注意:using
System.Security.Principal;
//获得当前Windows用户
WindowsIdentity curUser = WindowsIdentity.GetCurrent();
//用户SID
SecurityIdentifier sid = curUser.User;
//用户全称
NTAccount ntacc = (NTAccount)sid.Translate(typeof(NTAccount));
Console.WriteLine(sid.Value);
Console.WriteLine(ntacc.Value);
输出:
S-1-5-21-2376214308-3361272619-2153758801-1000
Mgen-PC\Mgen
3. 使用IPrincipal判断用户账户类型(支持用户账户控制(UAC)提示)
System.Security.Principal.IPrincipal接口代表定义用户对象的基本功能,其派生类WindowsPrincipal可以理解为代表Windows用户账户的权限或者用户类型。IPrincipal规定方法IsInRole(string
role)来判断用户是否属于指定的类型/角色。WindowsPrincipal类不仅实现了IPrincipal要求的IsInRole方法(参数是字符串),还重载了基于WindowsBuiltInRole枚举的IsInRole方法。WindowsBuiltInRole(MSDN:http://msdn.microsoft.com/zh-cn/library/system.security.principal.windowsbuiltinrole.aspx)包含了常见的Windows用户账户类比如:管理员,超级用户,用户,来宾用户……
当然IPrincipal是建立在IIdentity之上的,即只有知道了用户标识,才可以知道用户的基本功能。IPrincipal的Identity属性就返回IIdentity对象。当然派生的WindowsPrincipal则返回WindowsIdentity,后者则是IIdentity的派生类。
另外在Windows Vista,Windows
7和之后的Windows系统引入了用户账户控制(UAC:User Account
Control),即便用户是管理员账户,系统仿佛并不会将此用户运行的程序作为管理员权限而运行(Vista之前的系统是这样做的),任何可能影响系统安全的操作都会直接显示在屏幕上让用户判断是否可以继续,当用户同意执行后,该操作才可以以管理员方式进行。这样大大减少了某些恶意程序的幕后运行,因为很多恶意程序往往是费尽周折得到管理员权限运行后就可以为所欲为了。
下面这段代码可以判断利用WindowsPrincipal来判断用户是否具有管理员权限,运行后用户账户控制会提示是否给予程序管理员权限。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Security.Principal;
namespace Mgen.TTC
{
class Program
{
static void Main()
{
WindowsPrincipal winPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool admin = winPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
if (!admin)
{
if (!RunUAC(Process.GetCurrentProcess().MainModule.FileName))
{
Console.WriteLine("不是管理员");
return;
}
}
Console.WriteLine("是管理员");
}
static bool RunUAC(string fileName)
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Verb = "runas";
processInfo.FileName = fileName;
try
{
Process.Start(processInfo);
return true;
}
catch (System.ComponentModel.Win32Exception)
{ }
return false;
}
}
}
最新文章
- 1001 数组中和等于K的数对 1090 3个数和为0
- 玩转动态编译 - 高级篇:一,IL访问静态属性和字段
- JS for循环 闭包
- vi 使用入门
- [Effective JavaScript 笔记]全书总结
- 看StackOverflow如何用25台服务器撑起5.6亿的月PV(微软的架构)
- [ZOJ 1006] Do the Untwist (模拟实现解密)
- 大页(huge pages) 三大系列 ---计算大页配置参数
- build.gradle使用gradle.property中定义的字段及乱码问题的解决
- ajax 提交form格式 和 json格式
- SQL Server 2012 手动安装帮助文档+排错
- Struts2——通配符,Action Method_DMI
- Android AsyncTask将讲解
- scrapy-items
- Android编译系统入门(二)
- Kinect2.0获取数据
- Android移植学习笔记
- Alpha阶段敏捷冲刺---Day7
- Use UMDH to identify memory leak problem
- RelativeLayout用代码兑现布局
热门文章
- js进阶 9-16 如何实现多选框全选和取消
- 新技能 get —— Python 断点续传下载文件
- 20171028洛谷NOIP模拟
- svn: is already a working copy for a different url 解决办法
- 电子商务系统的设计与实现(七):前后端系统UI设计的一些思考
- spark rdd持久化的简单对比
- 一个git pull无法使用的问题
- 【27.48%】【codeforces 699D】 Fix a Tree
- Codeforces 85B. Embassy Queue【段树、馋】
- Matlab使用鼠标标注图像位置并返回坐标(标注图像ROI)