繁体   English   中英

C#WebClient表单身份验证

[英]C# WebClient FormsAuthentication

编辑:在底部。

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

我有一个WinForms应用程序,它无法连接到WebService(因为此程序需要身份验证)。 有一个示例如何在WebService和WinForms之间执行此身份验证? 我听说过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;
        }
}

当我想验证自己的身份时:

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)的正确字段中正确输入用户名和密码(管理员/管理员)。

你有什么事? (当然,我认为使用WebClient类更容易。。)(如果确实知道,NetworkCredentials仅可用于Windows身份验证,而Basic Authenticaiton is not吗?我站在我的一边,FormsAuthentication-URL上的相同)

编辑:

好的,我在这里看到了:

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

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

关闭响应时出现此错误:

webRequest.GetResponse().Close();

错误如下:

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

àSystem.Net.HttpWebRequest.GetResponse()

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

再次感谢社区!

好吧,我真的没有找到答案。

我这样做的方法是:使用“表单身份验证”保护所有网页。 但是不能保护我要使用的服务...(因为是的,这就是所有这些的目标!)

无论如何,非常感谢大家。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM