从ASP.Net页传递的NetworkCredential到HttpWebRequest的在C# [英] Passing NetworkCredential to HttpWebRequest in C# from ASP.Net Page

查看:1171
本文介绍了从ASP.Net页传递的NetworkCredential到HttpWebRequest的在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的HttpWebRequest来访问Web服务,并且我有路过凭据,请参阅下面code问题。我可以看到的凭据对象,NC,正在兴建中的调试器,以及在分配到request.Credentials,但是当我到达code将其故障的一个未授权的错误消息的最后一行。我有我们的服务器乡亲观看服务器上的请求,并且没有被传递凭证。难道我做错事的凭据对象,或者是有什么我需要做的,我不是在这里做什么?

 乌里requestUri = NULL;
    Uri.TryCreate(https://开头MyWebServer的/网页),
        UriKind.Absolute,出requestUri);    HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create
        (requestUri);    NC的NetworkCredential =
        新的NetworkCredential(用户,密码);    request.Credentials = NC;    request.Method = WebRequestMethods.Http.Get;
    HttpWebResponse响应=(HttpWebResponse)request.GetResponse();


解决方案

微软premier支持终于帮我使用CredentialCache类添加凭证和基本授权解决这个问题:

 的NetworkCredential NC =
        新的NetworkCredential(GetSetting(用户名),GetSetting(密码));
    CredentialCache缓存=新CredentialCache();    cache.Add(requestUri,基本,NC);    HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create(requestUri);

I'm trying to use HTTPWebRequest to access a web service, and am having problems passing credentials in, see code below. I can see the credentials object, nc, being built in the debugger, and also in the assignment to request.Credentials, but when I get to the last line of code it faults with a not authorized error message. I've had our server folks watch the request on the server, and there are no credentials being passed. Am I doing something wrong with the Credentials object, or is there something I need to do that I'm not doing here?

    Uri requestUri = null;
    Uri.TryCreate("https://mywebserver/webpage"), 
        UriKind.Absolute, out requestUri);

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create
        (requestUri);

    NetworkCredential nc =
        new NetworkCredential("user", "password");

    request.Credentials = nc;

    request.Method = WebRequestMethods.Http.Get;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

解决方案

Microsoft Premier Support finally helped me solve this problem by using the CredentialCache class to add the Credentials and the "Basic" authorization:

    NetworkCredential nc =
        new NetworkCredential(GetSetting("username"), GetSetting("password"));
    CredentialCache cache = new CredentialCache();

    cache.Add(requestUri, "Basic", nc);

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);

这篇关于从ASP.Net页传递的NetworkCredential到HttpWebRequest的在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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