[英]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.