测试访问网页的凭据

发布于 2024-09-08 01:41:56 字数 298 浏览 1 评论 0 原文

是否有一段不错且经过测试的代码可以用于此目的:

获取用户/通行证和 Web 服务的地址(asmx 页面)并检查用户/通行证是否有效。

我认为我应该使用 HTTPRequest 等来做到这一点,但我对该主题没有很好的了解,导致我当前的方法无法正常工作。

如果有一段很好的代码用于此目的,我很感激您向我指出这一点。

谢谢

PS:我没有在我的代码中使用 DefaultCredentials。因为我希望他们输入用户/通行证,所以现在我需要能够测试他们的用户/通行证,并在他们的凭据无效时向他们显示正确的消息。

Is there a nice and tested piece of code out there that I can use for this purpose:

get the user/pass and the address of a web service (asmx page) and check if the user/pass are valid or not.

I think I should use HTTPRequest,etc to do that but I do not have a good knowledge on that topic , causing my current method to not working properly.

If there is a good piece of code for this purpose I appreciate for pointing me to that.

Thanks

P.S: I am not using DefaultCredentials in my code. Since I want them to enter user/pass so now I need to be able to TEST their user/pass and show proper message to them if their credentials is not valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虚拟世界 2024-09-15 01:41:56

您可以使用 HttpWebRequest。凭据属性(取决于 Web 服务身份验证)和 CredentialCache 类

还有一些代码示例(来自谷歌):
检索 .NET 中的 HTTP 内容
结合 调用Web 服务动态使用 HttpWebRequest.Credentials

You can use the HttpWebRequest.Credentials Property (depends on the web service authentication) and the CredentialCache Class.

Also some code examples (from google):
Retrieving HTTP content in .NET
Combine Invoking Web Service dynamically using HttpWebRequest with .Credentials.

绝影如岚 2024-09-15 01:41:56
public bool TestCredentials(string url, string username, string password)
{
    var web = new WebClient();
    web.Credentials = new NetworkCredential(username,password);

    try
    {   
        web.DownloadData(url);
        return true;
    }
    catch (WebException ex)
    {
        var response = (HttpWebResponse)ex.Response;
        if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            return false;
        }

        throw;
    }   
}
public bool TestCredentials(string url, string username, string password)
{
    var web = new WebClient();
    web.Credentials = new NetworkCredential(username,password);

    try
    {   
        web.DownloadData(url);
        return true;
    }
    catch (WebException ex)
    {
        var response = (HttpWebResponse)ex.Response;
        if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            return false;
        }

        throw;
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文