FWH*_*FWH 10 c# passwords httplistener
我在这里面临一个问题,使用HttpListener.
当表格的请求
http://user:password@example.com/
Run Code Online (Sandbox Code Playgroud)
制作完成后,如何获取用户和密码?HttpWebRequest有一个Credentials属性,但是HttpListenerRequest没有它,我没有在它的任何属性中找到用户名.
谢谢您的帮助.
Mat*_*ley 22
您尝试做的是通过HTTP基本身份验证传递凭据,我不确定HttpListener是否支持username:password语法,但如果是,则需要指定您首先接受基本身份验证.
HttpListener listener = new HttpListener();
listener.Prefixes.Add(uriPrefix);
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
listener.Start();
Run Code Online (Sandbox Code Playgroud)
收到请求后,您可以通过以下方式提取用户名和密码:
HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
Console.WriteLine(identity.Name);
Console.WriteLine(identity.Password);
Run Code Online (Sandbox Code Playgroud)
以下是可与HttpListener一起使用的所有支持的authenitcation方法的完整说明.
获取Authorization
标题。它的格式如下
Authorization: <Type> <Base64-encoded-Username/Password-Pair>
Run Code Online (Sandbox Code Playgroud)
例子:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Run Code Online (Sandbox Code Playgroud)
用户名和密码以冒号分隔(在本例中为Aladdin:open sesame
),然后是 B64 编码。
您需要首先启用基本身份验证:
listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
Run Code Online (Sandbox Code Playgroud)
然后在您的 ProcessRequest 方法中您可以获取用户名和密码:
if (context.User.Identity.IsAuthenticated)
{
var identity = (HttpListenerBasicIdentity)context.User.Identity;
Console.WriteLine(identity.Name);
Console.WriteLine(identity.Password);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13372 次 |
最近记录: |