简体   繁体   English

如何获取当前用户的用户名,密码和域

[英]How to get username, password and domain of current user

I develop a windows service to interop with reporting service and so i need to form NetworkCredential to pass to SSRS. 我开发了Windows服务以与报表服务进行互操作,因此我需要组建NetworkCredential才能传递给SSRS。 The question is - how can i get username, SecureString and domain of user that runs the service? 问题是-我如何获取运行服务的用户名,SecureString和用户域?

I'll be gratefull for any help! 如果有任何帮助,我将不胜感激!

You can get the current user by: 您可以通过以下方式获取当前用户:

System.Security.Principal.WindowsIdentity.GetCurrent().Name;

If I remember correctly, this also has a prefix of the domain on the form domain\\username 如果我没记错的话,它在domain\\username的形式上也有域的前缀

You cannot retrieve the password for the current logged in user. 您无法获取当前登录用户的密码。 What you can do however, is to specify which user the windows service runs as. 但是,您可以指定Windows服务的运行用户。 That way you will know the password, and be able to link that account, and grant the appropriate access throughout your domain. 这样,您将知道密码,并能够链接该帐户,并在整个域中授予适当的访问权限。

Note that if you sometime in the future decide to change password to the user account, you will manually have to update the credentials on the service. 请注意,如果您将来决定更改用户帐户的密码,则必须手动更新服务上的凭据。

To have a complete answer including FirstName, LastName and Domain we can use below code: 要获得包括名字,姓氏和域的完整答案,我们可以使用以下代码:

var domain = Environment.UserDomainName;
UserPrincipal userPrincipal = UserPrincipal.Current;
var firstname = userPrincipal.GivenName;
var lastname = userPrincipal.SurName
var username_id = userPrincipal.SamAccountName;

You need to add System.DirectoryServices.AccountManagement as assembly reference. 您需要添加System.DirectoryServices.AccountManagement作为程序集引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "如何获取用户名@域格式的当前 Windows 用户名?" - How do I get the current windows user's name in username@domain format? 如何检查[UserName]和[Password]的用户是否是[DomainName]的域管理员而没有模拟? - How to check if user with [UserName] and [Password] is domain administrator of [DomainName] without impersonation? 如何获取UWP中当前登录用户的用户名? - How to get the username of the current logged in user in UWP? 如何从Windows操作系统获取当前用户名和密码。 - How get Current Username and password from windows operating System. 如何在指定用户名和域时获取用户的广告组 - How to get AD groups a user is in specifying username and domain 如何通过ldap中的域名获取用户的用户名和SID - How to get username and SID for user by a domain name in ldap 如何在Windows应用程序中为当前登录的用户存储用户设置(用户名,密码) - How to store user settings (username, password) in a Windows application for the current logged in user UWP:获取当前的用户名以向用户提供权限 - UWP : Get the current UserName to provide rights to the user 如何在Store Apps中以编程方式获取当前用户的域 - How to get the Domain of Current User programmatically in Store Apps 如何在mvc中获取本地系统域的当前登录用户 - How to get current logged in user of Domain of local system in mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM