C# 在 Windows 身份验证应用程序中从当前用户获取网络凭据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11414266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:47:29  来源:igfitidea点击:

Obtain Network Credentials from Current User in Windows Authentication Application

c#windows-authenticationaxaptabusiness-connector

提问by CallumVass

I was wondering whether it was possible to obtain the current user object and get their credentials so that I can pass them along to a NetworkCredentialobject which I am using to connect to my AX .NET Business Connector. As, at the moment I'm having to specify it connect as a specific user which I set when I instantiate a NetworkCredentialobject:

我想知道是否可以获取当前用户对象并获取他们的凭据,以便我可以将它们传递给NetworkCredential我用来连接到我的 AX .NET 业务连接器的对象。因为,目前我必须将它指定为我在实例化NetworkCredential对象时设置的特定用户:

private NetworkCredential nc = new NetworkCredential("myUser", "myPassword", "myDomain");

private NetworkCredential nc = new NetworkCredential("myUser", "myPassword", "myDomain");

I was hoping to do something like: private NetworkCredential nc = (NetworkCredential)HttpContext.User;but obviously that won't work...

我希望做类似的事情:private NetworkCredential nc = (NetworkCredential)HttpContext.User;但显然这行不通......

That way, it's easier to keep track of which user has created a sales order for example, as at the moment everything gets created by the user I have specified..

这样,例如,可以更轻松地跟踪哪个用户创建了销售订单,因为目前所有内容都是由我指定的用户创建的。

采纳答案by Damien_The_Unbeliever

CredentialCache.DefaultNetworkCredentials?

CredentialCache.DefaultNetworkCredentials?

The credentials returned by DefaultNetworkCredentialsrepresents the authentication credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application.

返回的凭据DefaultNetworkCredentials表示应用程序在其中运行的当前安全上下文的身份验证凭据。对于客户端应用程序,这些通常是运行该应用程序的用户的 Windows 凭据(用户名、密码和域)。

回答by Jaime Torres

I don't fully understand your question, but is your call coming from ASP.NET that you require the credentials? You could attempt:

我不完全理解您的问题,但是您的电话是否来自 ASP.NET,您需要凭据?你可以尝试:

Uri uri = new Uri("http://tempuri.org/");
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(uri, "Basic");

Assuming your user has already authenticated via a Membership Provider.

假设您的用户已经通过会员提供商进行了身份验证。