原文:浅谈.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;

}

}

}

浅谈.NET(C#)与Windows用户账户信息的获取的更多相关文章

  1. (C#)与Windows用户账户信息的获取

    Console.WriteLine(Environment.UserName); //计算机NetBIOS名称 Console.WriteLine(Environment.MachineName); ...

  2. UWP 应用获取各类系统、用户信息 (1) - 设备和系统的基本信息、应用包信息、用户数据账户信息和用户账户信息

    应用开发中,开发者时常需要获取一些系统.用户信息用于数据统计遥测.问题反馈.用户识别等功能.本文旨在介绍在 Windows UWP 应用中获取一些常用系统.用户信息的方法.示例项目代码可参见 Gith ...

  3. usermod---修改用户账户信息

    usermod可用来修改用户帐号的各项设定. 语法 usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数 ...

  4. linux中查找用户账户信息和登录信息的11中方法

    摘自:开源中国 微信公众号 1. id 2. groups 3. finger 4.getent 5. grep 6. lslogins 7..users 8. who 9. w 10. last或者 ...

  5. SAP中关于用户IP信息的获取(转载)

    SAP中如何获取登录用户的IP? 或如何查看哪些IP登录到SAP中: 在Table: USR41中查看,具体字段的说明如下: MANDT   ---   ClientBNAME   ---   登录的 ...

  6. 设置Cookie,登录记住用户登录信息,获取用户登录过得信息

    function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Da ...

  7. 背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意

    [源码下载] 背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意 作者:webabcd 介绍背水一战 Windows 10 之 用户和账号 获取用户的信息 获 ...

  8. CentOS 7 用户账户配置

    说明: 1.这篇博文记录的是CentOS 7 用户账户的配置,包括添加用户.添加用户组.删除用户.删除用户组等.其中包括分析用户的配置文件.目录以及对安全的思考. 2.用户配置方面CentOS 7与以 ...

  9. 利用Scrapy爬取所有知乎用户详细信息并存至MongoDB

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者 :崔庆才 本节分享一下爬取知乎用户所有用户信息的 Scrapy 爬虫实战. 本节目标 本节要实现的内容有 ...

随机推荐

  1. [React Router v4] Create Basic Routes with the React Router v4 BrowserRouter

    React Router 4 has several routers built in for different purposes. The primary one you will use for ...

  2. [NPM] Pull out npm scripts into another file with p-s

    A technique you might use once you start having lots of npm scripts is to use a node package that al ...

  3. 孔雀翎----《Programming C# 》中国版 文章4版

    孔雀翎----<Programming C# >中国版 文章4版 页:http://blog.csdn.net/21aspnet/           时间:2007.8.7 电子工业出版 ...

  4. CocoaPods详解之(一)----使用篇

    CocoaPods详解之----使用篇 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/18737437 一.什么是Coc ...

  5. Windows消息:WM_USER与WM_APP的区别

    Windows消息范围及意义 #define WM_USER 0x0400 #define WM_APP 0x8000 0到WM_USER-1 Messages reserved for use by ...

  6. 修改MessageBox的标题的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 1.用Win API的::MessageBox或CWnd::MessageBox代替AfxMessageBox. 2. ...

  7. 为什么台湾人工智能可能抢输大陆?(XPU时代来临)

    到了 2020 年,每 3 支手机,就会有一支内建有 AI 芯片. 但目前浮出水面的 AI 芯片新创,几乎都是大陆公司. 为什么台湾这回选择缺席? 「我听说 CPU.GPU,没有听过 NPU? 」11 ...

  8. 系统性能指标之 vmstat

    系统性能指标 top top - 19:59:04 up 219 days, 21:51, 2 users, load average: 0.06, 0.06, 0.05 Tasks: 84 tota ...

  9. centos 6 防火墙开启端口无效问题

    昨天尝试redis在centos的安装,配置文件都检查了,外网就是不能访问 #添加端口开启 $ iptables -A INPUT -p tcp --dport 6379 -j ACCEPT #保存配 ...

  10. C Shell中的变量数组

    今天刚刚在看一点C Shell的内容,发现一个挺好玩的东西!就是环境变量可以像数组那样来设置!具体设置语法如下: set variable=(element1 element2 ...) //注意元素 ...