HttpListener:如何获取http用户名和密码? [英] HttpListener: how to get http user and password?

查看:396
本文介绍了HttpListener:如何获取http用户名和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用HttpListener时遇到问题.

I'm facing a problem here, with HttpListener.

当请求形式为

http://user:password@example.com/

完成后,如何获取用户名和密码? HttpWebRequest有一个Credentials属性,但是HttpListenerRequest没有它,而且我在它的任何属性中都没有找到用户名.

is made, how can I get the user and password ? HttpWebRequest has a Credentials property, but HttpListenerRequest doesn't have it, and I didn't find the username in any property of it.

感谢您的帮助.

推荐答案

您正在尝试通过HTTP基本身份验证传递凭据,我不确定HttpListener是否支持username:password语法,但是如果是的,您需要指定您首先接受基本身份验证.

What you're attempting to do is pass credentials via HTTP basic auth, I'm not sure if the username:password syntax is supported in HttpListener, but if it is, you'll need to specify that you accept basic auth first.

HttpListener listener = new HttpListener();
listener.Prefixes.Add(uriPrefix);
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
listener.Start();

收到请求后,您可以使用以下方式提取用户名和密码:

Once you receive a request, you can then extract the username and password with:

HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
Console.WriteLine(identity.Name);
Console.WriteLine(identity.Password);

这里是所有受支持的身份验证方法的完整说明可以与HttpListener一起使用.

Here's a full explanation of all supported authenitcation methods that can be used with HttpListener.

这篇关于HttpListener:如何获取http用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆