C#WebClient表单身份验证 [英] C# WebClient FormsAuthentication

查看:262
本文介绍了C#WebClient表单身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在底部.

我有一个使用WebService的站点. 使用以下方式启用身份验证: "如何:实现简单表单身份验证".

I've this site which uses a WebService. Authentication is enabled using this way : "How to: Implement Simple Forms Authentication".

我有一个WinForms应用程序,它无法连接到WebService(因为此程序需要身份验证). 有一个示例如何在WebService和WinForms之间执行此身份验证?我听说过System.Net.HttpWebRequest,但没有找到使用它的示例.

I've a WinForms application who cant connect to the WebService (because this one needs an authentication). There is an example how to perform this authentication between the WebService and the WinForms ? I heard about System.Net.HttpWebRequest, but I didn't find an example to use it .

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");
}

问题可能是,我实际上没有在Logon.aspx ...(UserMail和UserPass)的正确字段中输入正确的用户名和密码(管理员/管理员).

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).

你怎么办? (当然,我认为使用WebClient类更容易.) (如果真的知道的话,NetworkCredentials仅适用于Windows身份验证,而Basic Authenticaiton不是吗?我站在我的一边,FormsAuthentication-与我提供的URL相同)

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)

好的,我在这里看到了:

OK, I saw here :

http://odetocode.com/articles/162.aspx

因此,我还尝试执行HTTPWebRequest在页面上看到它.

And so, I also tried to perform the HTTPWebRequest saw it on the page.

关闭响应时出现此错误:

I get this error when closing the response :

webRequest.GetResponse().Close();

错误如下:

System.Net.WebException: Le serveur distant a retourné une erreur : (500) Erreur interne du serveur.

àSystem.Net.HttpWebRequest.GetResponse()

à System.Net.HttpWebRequest.GetResponse()

显然,我的WebClient POST请求和HTTPWebRequest确实存在问题,可能是在IIS服务器上配置的问题吗?

Apparently there is really a problem with my WebClient POST request and my HTTPWebRequest, may be something to configure on the IIS server ?

再次感谢社区!

推荐答案

好的,我真的没有找到答案.

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 ! )

非常感谢大家.

这篇关于C#WebClient表单身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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