ASP.NET 兼容模式下的 WCF 服务 - 传递用户名/密码的最简单方法是什么

发布于 2024-07-10 14:51:43 字数 537 浏览 7 评论 0原文

我有一个Web服务(WCF或ASMX并不重要)...我制作了一个控制台应用程序,右键单击,添加服务引用。 到目前为止,一切都很好。

但是,我一生都无法将“安全”凭据传递给我的服务。 这是我的客户端代码:

var client = new MyClient();

client.ClientCredentials.UserName.UserName = "bob";
client.ClientCredentials.UserName.Password = "123!!";

client.HelloWorld();

client.Close();

但是在服务器上,无论我做什么(aspnetcompant 模式打开和关闭、wcf 服务、asmx 服务、自定义 http 处理程序等)...我找不到“bob:123!!” 任何地方。 不在标头中,不在 HttpContext.Current.User.Identiy.Name 中,不在 Thread.CurrentPrincipal 中......什么都没有。

我在这里缺少什么?

I have a web service (WCF or ASMX doesn't matter)... I have made a Console application, right-clicked, added service referrence. So far, so good.

However, I cannot for the life of me pass "security" credentials across to my service. This is my client code:

var client = new MyClient();

client.ClientCredentials.UserName.UserName = "bob";
client.ClientCredentials.UserName.Password = "123!!";

client.HelloWorld();

client.Close();

But on the server, no matter what I do (aspnetcompant mode on and off, wcf service, asmx service, custom http handlers, etc)... I can't find 'bob:123!!' anywhere. Not in headers, not in HttpContext.Current.User.Identiy.Name, not in Thread.CurrentPrincipal... nothing.

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

行至春深 2024-07-17 14:51:43

您是否检查过有关自定义验证器的这篇文章:http://www.leastprivilege.com/FinallyUsernamesOverTransportAuthenticationInWCF.aspx?

另外,当我从事这项工作时,对我有帮助的一件事是禁用 Web 服务器对该目录的匿名访问。 当您未经身份验证时,HttpContext.Current.User.Identity.Name 始终返回空字符串。 因此,看起来您的身份验证正确,只是无法“找到”用户名,但实际上您是匿名登录的。 至少在禁用匿名访问的情况下,您会遇到异常,并且可以更轻松地弄清楚身份验证方面,这是最难的部分。

Have you checked this article on custom validators: http://www.leastprivilege.com/FinallyUsernamesOverTransportAuthenticationInWCF.aspx?

Also, one thing that helped me when I worked on this was to disable anonymous access to that directory for the web server. HttpContext.Current.User.Identity.Name always returns an empty string when you are not authenticated. Thus it can look like you are authenticating correctly and just can't "find" the username, but really you are logged in anonymously. At least with anon access disabled, you will get an exception and can figure out the authentication side more easily, which is the hardest part.

城歌 2024-07-17 14:51:43

是的,我有,显然如果你不使用 SSL,.Net 会抛出异常。 显然,如果没有 SSL,您无法做我想做的事。

Yes I have, and apparently if you don't use SSL, .Net throws an exception. So apparently you can't do what I want without SSL.

初心未许 2024-07-17 14:51:43

请参阅:http://developers.de/blogs/damir_dobric/ archive/2006/07/31/890.aspx 有关 TransportCredentialOnly 的信息,以便能够在没有 SSL 的情况下传递用户名和密码。

这只应在测试环境中完成,因为您以纯文本形式公开用户的密码。

See: http://developers.de/blogs/damir_dobric/archive/2006/07/31/890.aspx for information about TransportCredentialOnly to be able to pass usernames and passwords without SSL.

This should only be done in TESTING environments, as you are exposing the user's password in plain text.

往日情怀 2024-07-17 14:51:43

您是否在端点绑定上配置了 UserName(消息安全)或 Basic(传输安全)客户端凭据类型?

Have you configured the UserName (message security) or Basic (transport security) client credential type on the endpoint binding?

风渺 2024-07-17 14:51:43

据我了解,WCF 进行消息级身份验证,而不是集成身份验证。 无论哪种情况,我不确定传递凭据是否可以在没有 SSL 的情况下完成...

您是否尝试过在端点上打开匿名身份验证(通过 IIS)? 据我了解,WCF 使用消息级安全性(在消息标头中传递的用户名/密码)而不是 HTTP 标头(在 Windows Integrated、SPNego 等集成身份验证方法中使用)

我找到的一些链接...

From what I understand, WCF does message level auth, not integrated auth. In either case, I'm not sure passing credentials is something you can do without SSL...

Have you tried turning on Anonymous authentication on the endpoint (via IIS)? From what I understand, WCF uses message level security (username/password passed in the message headers) rather than HTTP headers (used in integrated auth methods like Windows Integrated, SPNego, etc.)

some links I found...

自由如风 2024-07-17 14:51:43

如果我正确理解您的问题,则您已成功为 WCF 服务配置用户名身份验证(抱歉 - 我将重点关注 WCF),并且您希望在服务的方法中访问用户名和密码。

您可以通过ServiceSecurityContext.Current.PrimaryIdentity访问用户名,我不相信您可以轻松访问密码。

我在网上找到的一个建议(我不太喜欢)是让您的自定义验证器保留用户名和密码的字典,稍后您可以根据给定的用户名从代码中访问该字典。

我不喜欢这个有几个原因 - 一 - 你不应该真正有权访问密码,二 - 我不确定这有多安全,三 - 是否可能有重复的用户名(我知道一些系统(如果是这种情况)您可能最终会得到错误的结果。

If I understand your question correctly, you have succesfully configured username authentication for a WCF service (sorry - I'll focus on WCF) and you wish to gain access to the username and password within your serivce's methods.

You can access the username through ServiceSecurityContext.Current.PrimaryIdentity, I don't believe you can access the password easily.

One suggestion I found on the web, which I don't really like, is for your custom validator to keep a dictionary of username and passwords, which you could access later from your code given a username.

I don't like this for several reasons - one - you shouldn't really have access to passwords anyway, two - I'm not sure how secure this is, and three - if it's possible to have duplicate usernames (I know of some systems where this is the case) you might end up getting the wrong one.

花开柳相依 2024-07-17 14:51:43

你尝试过访问这个吗? (特别是如果您使用自己的身份验证系统以及自己的用户名密码和证书验证器。

System.ServiceModel.ServiceSecurityContext.Current.PrimaryIdentity.Name

您可以获取调用者的姓名,但不能获取密码。

have you tried accessing this? (especially if you are using your own authentication system and your own UsernamePasssword and Certificate validators.

System.ServiceModel.ServiceSecurityContext.Current.PrimaryIdentity.Name

you can get the caller's name but not the password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文