在C#中检索并使用服务“以身份登录”凭据(Retrieve and use service 'Log on as' credentials in C#)

编程入门 行业动态 更新时间:2024-10-23 09:41:15
在C#中检索并使用服务“以身份登录”凭据(Retrieve and use service 'Log on as' credentials in C#)

我目前正在编写一个Windows服务,需要与内部部署的Exchange服务器进行交互。 非常正确,我们得出的结论是,对Exchange凭据进行硬编码是一个非常糟糕的主意,但是将它们以纯文本形式存储在配置文件中也是如此。 我们得出结论,创建一个拥有相关邮箱的服务帐户应该满足我们的要求,但是我遇到了以下问题:

我已经开始使用ManagementObjectSearcher来获取在我的PC上运行服务的帐户的用户名,但是我不知道如何,或者即使我可以获得密码。 我试过这个:

class Program { public static ManagementObjectSearcher Searcher = new ManagementObjectSearcher(new SelectQuery("select * from Win32_Service where name = 'netlogon'")); static void Main(string[] args) { ManagementObjectCollection Response = Searcher.Get(); Console.WriteLine(Response.Count); foreach (ManagementObject Item in Response) { Console.WriteLine(Item.ToString()); } Console.ReadLine(); } }

不幸的是,我没有收到密码。 EWS API文档( https://msdn.microsoft.com/en-us/library/office/dn626019(v=exchg.150).aspx )提出了一些建议,我可以获取当前登录用户的凭据,但我查看了引用的代码示例,并且没有找到关于这是如何可行的建议。

为清楚起见,我可以获得用户名,这没关系。 我需要密码...任何帮助将不胜感激!

I'm currently writing a Windows Service which will be required to interact with an on-premise Exchange server. Quite rightly, we've come to the conclusion that hard-coding the Exchange credentials is a very bad idea, but so is storing them in a config file in plain text. We've come to the conclusion that creating a service account that owns the mailbox in question should satisfy our requirements, however I've hit the following problem:

I've started to use a ManagementObjectSearcher to get the Username of the account running a service on my PC, however I'm not sure how to, or even if, I can get the password. I've tried this:

class Program { public static ManagementObjectSearcher Searcher = new ManagementObjectSearcher(new SelectQuery("select * from Win32_Service where name = 'netlogon'")); static void Main(string[] args) { ManagementObjectCollection Response = Searcher.Get(); Console.WriteLine(Response.Count); foreach (ManagementObject Item in Response) { Console.WriteLine(Item.ToString()); } Console.ReadLine(); } }

Unfortunately, I don't get a Password back. There's some suggestion from the EWS API documentation (https://msdn.microsoft.com/en-us/library/office/dn626019(v=exchg.150).aspx) that I can grab the credentials of the currently logged in user, but I looked through the referenced code samples and can find no suggestion of how this is doable.

For clarity, I can get the username, that's fine. I need the password... Any help would be appreciated!

最满意答案

似乎有两个可接受的答案:

@rene建议使用Microsoft CredentialCache类型来返回当前登录用户的网络凭据,这在服务上下文中似乎只是“登录为”凭据。 详细信息请访问: https : //msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultnetworkcredentials(v = vs.110).aspx

第二个是我发现的,它只涉及不向EWS API ExchangeService类型添加凭据,在这种情况下,运行服务的用户的凭据将自动传递到Exchange。

It seems there are two acceptable answers:

One that @rene proposed involves using the Microsoft CredentialCache type to return the network credentials of the currently logged in user, which in a service context seems to just be the 'log on as' credentials. Details found here: https://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultnetworkcredentials(v=vs.110).aspx

The second is one that I Found, which involves simply not adding credentials to the EWS API ExchangeService type, in which case the credentials of the users running the service will automatically be passed through to Exchange.

更多推荐

本文发布于:2023-08-03 12:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:凭据   身份   Retrieve   credentials   Log

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!