[英]C# WebClient FormsAuthentication
EDIT: At the bottom . 编辑:在底部。
I've this site which uses a WebService. 我有一个使用WebService的站点。 Authentication is enabled using this way : " How to: Implement Simple Forms Authentication ".
使用以下方式启用身份验证:“ 如何:实现简单表单身份验证 ”。
I've a WinForms application who cant connect to the WebService (because this one needs an authentication). 我有一个WinForms应用程序,它无法连接到WebService(因为此程序需要身份验证)。 There is an example how to perform this authentication between the WebService and the WinForms ?
有一个示例如何在WebService和WinForms之间执行此身份验证? I heard about System.Net.HttpWebRequest, but I didn't find an example to use it .
我听说过System.Net.HttpWebRequest,但没有找到使用它的示例。
class CookieWebClient : WebClient
{
public CookieContainer CookieContainer { get; private set; }
/// <summary>
/// This will instanciate an internal CookieContainer.
/// </summary>
public CookieWebClient()
{
this.CookieContainer = new CookieContainer();
}
/// <summary>
/// Use this if you want to control the CookieContainer outside this class.
/// </summary>
public CookieWebClient(CookieContainer cookieContainer)
{
this.CookieContainer = cookieContainer;
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
if (request == null) return base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
}
and when I want to authenticate myself : 当我想验证自己的身份时:
using (var client = new CookieWebClient())
{
var values = new NameValueCollection
{
{ "UserEmail.Text", "Administrator" },
{ "UserPass.Text", "admin" },
};
client.UploadValues("http://localhost:54787/logon.aspx", values);
// If the previous call succeeded we now have a valid authentication cookie
// so we could download the protected page
string result = client.DownloadString("http://localhost:54787/MyWantedPage.aspx");
}
The problem is maybe, that I don't really put the right user and password (Administrator/admin) in the right field on the Logon.aspx... (UserMail and UserPass). 问题可能是,我没有在Logon.aspx ...(UserMail和UserPass)的正确字段中正确输入用户名和密码(管理员/管理员)。
What do you thing ? 你有什么事? (of course I assume is easier to use WebClient class.. ) (If I really got it, NetworkCredentials is only available with Windows Authentication and Basic Authenticaiton isnt ? I've in my side, FormsAuthentication - the same one on the URL I gave)
(当然,我认为使用WebClient类更容易。。)(如果确实知道,NetworkCredentials仅可用于Windows身份验证,而Basic Authenticaiton is not吗?我站在我的一边,FormsAuthentication-URL上的相同)
EDIT : 编辑:
OK, I saw here : 好的,我在这里看到了:
http://odetocode.com/articles/162.aspx http://odetocode.com/articles/162.aspx
And so, I also tried to perform the HTTPWebRequest saw it on the page. 因此,我还尝试执行HTTPWebRequest在页面上看到它。
I get this error when closing the response : 关闭响应时出现此错误:
webRequest.GetResponse().Close();
the error is below : 错误如下:
System.Net.WebException: Le serveur distant a retourné une erreur : (500) Erreur interne du serveur.
à System.Net.HttpWebRequest.GetResponse() àSystem.Net.HttpWebRequest.GetResponse()
Apparently there is really a problem with my WebClient POST request and my HTTPWebRequest, may be something to configure on the IIS server ? 显然我的WebClient POST请求和HTTPWebRequest确实存在问题,可能是在IIS服务器上配置的问题?
Thanks again the community ! 再次感谢社区!
OK, I don't really find an answer about that. 好吧,我真的没有找到答案。
The way I do it is : protect all the Web pages using Forms Authentication. 我这样做的方法是:使用“表单身份验证”保护所有网页。 But not protect the Service I wnat to consume... (because yes, that was the goal of all of this ! )
但是不能保护我要使用的服务...(因为是的,这就是所有这些的目标!)
Thank you very much anyway to all of you. 无论如何,非常感谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.