本文整理汇总了C#中System.Net.CredentialCache.Add方法的典型用法代码示例。如果您正苦于以下问题:C# CredentialCache.Add方法的具体用法?C# CredentialCache.Add怎么用?C# CredentialCache.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.CredentialCache
的用法示例。
在下文中一共展示了CredentialCache.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFileFromSite
/// <summary>
/// method to download the contents of a file from a remote URI
/// </summary>
/// <param name="ftpUri"></param>
/// <param name="user"></param>
/// <param name="pass"></param>
/// <returns></returns>
public static string GetFileFromSite(Uri ftpUri, string user, string pass)
{
string fileString = string.Empty;
// The serverUri parameter should start with the ftp:// scheme.
if (ftpUri.Scheme != Uri.UriSchemeFtp)
{
return string.Empty;
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();
// This example assumes the FTP site uses anonymous logon.
NetworkCredential nc = new NetworkCredential(user, pass);
CredentialCache cc = new CredentialCache();
cc.Add(ftpUri, "Basic", nc);
request.Credentials = cc;
try
{
byte[] newFileData = request.DownloadData(ftpUri.ToString());
fileString = System.Text.Encoding.UTF8.GetString(newFileData);
}
catch (WebException e)
{
m_logger.WriteToLogFile("FtpUtils::GetFileFromSite();ECaught: " + e.Message);
}
return fileString;
}
示例2: multiple_credentials_via_user_supplied_credential_cache
public void multiple_credentials_via_user_supplied_credential_cache()
{
var cred = new OAuth2Client.OAuth2Credential(
"apiv1",
new OAuth2Client.Storage.JsonFileStorage(
@"C:\Projects\github\oauth2_files\local_versiononeweb_client_secrets.json",
@"C:\Projects\github\oauth2_files\local_versiononeweb_creds.json"),
null
);
var cache = new CredentialCache();
var simpleCred = new NetworkCredential(V1Username, V1Password);
cache.Add(V1Uri, "Basic", simpleCred);
cache.Add(V1Uri, "Bearer", cred);
var windowsIntegratedCredential = CredentialCache.DefaultNetworkCredentials;
// TODO explore: CredentialCache.DefaultCredentials
// Suppose for some weird reason you just wanted to support NTLM:
cache.Add(V1Uri, "NTLM", windowsIntegratedCredential);
var connector = new VersionOneAPIConnector(V1Path, cache);
connector.HttpGet("rest-1.v1/Data/Member/20");
}
示例3: TestLookupCredentialWithCredentialCache
public void TestLookupCredentialWithCredentialCache()
{
var credentials = new CredentialCache();
var credPopuser1 = new NetworkCredential("popuser1", "password");
var credPopuser2 = new NetworkCredential("popuser2", "password");
var credImapuser1 = new NetworkCredential("imapuser1", "password");
var credImapuser2 = new NetworkCredential("imapuser2", "password");
credentials.Add("localhost", 110, string.Empty, credPopuser1);
credentials.Add("localhost", 110, "cram-md5", credPopuser2);
credentials.Add("localhost", 143, string.Empty, credImapuser1);
credentials.Add("localhost", 143, "digest-md5", credImapuser2);
Assert.AreEqual(credPopuser1, credentials.LookupCredential("localhost", 110, "popuser1", null));
Assert.AreEqual(credPopuser2, credentials.LookupCredential("localhost", 110, "popuser2", "CRAM-MD5"));
Assert.AreEqual(credImapuser1, credentials.LookupCredential("localhost", 143, "imapuser1", null));
Assert.AreEqual(credImapuser2, credentials.LookupCredential("localhost", 143, "imapuser2", "DIGEST-MD5"));
Assert.AreEqual(credPopuser1, credentials.LookupCredential("localhost", 110, null, null));
Assert.AreEqual(credPopuser2, credentials.LookupCredential("localhost", 110, null, "CRAM-MD5"));
Assert.IsNull(credentials.LookupCredential("localhost", 110, null, "DIGEST-MD5"));
Assert.AreEqual(credImapuser1, credentials.LookupCredential("localhost", 143, null, null));
Assert.AreEqual(credImapuser2, credentials.LookupCredential("localhost", 143, null, "DIGEST-MD5"));
Assert.IsNull(credentials.LookupCredential("localhost", 143, null, "CRAM-MD5"));
}
示例4: conexion_rest
public object conexion_rest(string type_request,string controller,object jsonObject,int id)
{
string json = JsonConvert.SerializeObject(jsonObject);
string host = "http://localhost:3000/";
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(host + controller + ".json");
if (type_request == "post")
{
request.Method = WebRequestMethods.Http.Post;
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentType = "application/json";
request.Accept = "application/json";
request.ContentLength = data.Length;
StreamWriter postStream = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
postStream.Write(json);
postStream.Close();
}
if (type_request == "get")
{
if (id != -1)
{
request = (HttpWebRequest)WebRequest.Create(host + controller + "/"+id);
}
request.Method = WebRequestMethods.Http.Get;
}
if (type_request == "Put")
{
request = (HttpWebRequest)WebRequest.Create(host + controller + "/"+id);
request.Method = WebRequestMethods.Http.Put;
request.ContentType = "application/json";
request.Accept = "application/json";
byte[] data = Encoding.UTF8.GetBytes(json);
request.ContentLength = data.Length;
StreamWriter postStream = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
postStream.Write(json);
postStream.Close();
}
NetworkCredential credentials = new NetworkCredential("username", "123");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(host),"Basic", credentials);
cache.Add(new Uri(host),"Negotiate",credentials);
request.Credentials = cache;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string html = sr.ReadToEnd();
object json_respond = JsonConvert.DeserializeObject(html);
return json;
//return html;
}
示例5: GetVssCredentials
public override VssCredentials GetVssCredentials(IHostContext context)
{
ArgUtil.NotNull(context, nameof(context));
Tracing trace = context.GetTrace(nameof(NegotiateCredential));
trace.Info(nameof(GetVssCredentials));
ArgUtil.NotNull(CredentialData, nameof(CredentialData));
// Get the user name from the credential data.
string userName;
if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.UserName, out userName))
{
userName = null;
}
ArgUtil.NotNullOrEmpty(userName, nameof(userName));
trace.Info("User name retrieved.");
// Get the password from the credential data.
string password;
if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Password, out password))
{
password = null;
}
ArgUtil.NotNullOrEmpty(password, nameof(password));
trace.Info("Password retrieved.");
// Get the URL from the credential data.
string url;
if (!CredentialData.Data.TryGetValue(Constants.Agent.CommandLine.Args.Url, out url))
{
url = null;
}
ArgUtil.NotNullOrEmpty(url, nameof(url));
trace.Info($"URL retrieved: {url}");
// Create the Negotiate and NTLM credential object.
var credential = new NetworkCredential(userName, password);
var credentialCache = new CredentialCache();
switch (Constants.Agent.Platform)
{
case Constants.OSPlatform.Linux:
case Constants.OSPlatform.OSX:
credentialCache.Add(new Uri(url), "NTLM", credential);
break;
case Constants.OSPlatform.Windows:
credentialCache.Add(new Uri(url), "Negotiate", credential);
break;
}
VssCredentials creds = new VssClientCredentials(new WindowsCredential(credentialCache));
trace.Verbose("cred created");
return creds;
}
示例6: multiple_credentials_via_user_supplied_credential_cache
public void multiple_credentials_via_user_supplied_credential_cache()
{
var simpleCred = new NetworkCredential(_username, _password);
var windowsIntegratedCredential = CredentialCache.DefaultNetworkCredentials;
var cache = new CredentialCache();
var uri = new Uri(_prefix);
cache.Add(uri, "Basic", simpleCred);
// Suppose for some weird reason you just wanted to support NTLM:
cache.Add(uri, "NTLM", windowsIntegratedCredential);
var connector = new VersionOneAPIConnector(_prefix, cache);
using (connector.GetData(Path)) ;
}
示例7: Get
/// <summary>
/// <para>Issue an HTTP GET request to the given <paramref name="requestUrl"/>.</para>
/// <para>The <paramref name="requestUrl"/> can contain query parameters
/// (name=value pairs).</para>
/// </summary>
/// <param name="requestUrl">The URL to issue the GET request to (with optional query
/// parameters)</param>
/// <returns>The server response.</returns>
/// <exception cref="HttpUtilsException">If an error issuing the GET request or parsing
/// the server response occurs.</exception>
public string Get(String requestUrl)
{
try
{
Uri requestUri = new Uri(requestUrl);
HttpWebRequest apiRequest = WebRequest.Create(requestUri) as HttpWebRequest;
apiRequest.Method = "GET";
apiRequest.KeepAlive = false;
if (username != null && password != null)
{
CredentialCache authCredentials = new CredentialCache();
authCredentials.Add(requestUri, "Basic", new NetworkCredential(username, password));
apiRequest.Credentials = authCredentials;
}
// Get response
string apiResponse = "";
using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
apiResponse = reader.ReadToEnd();
}
return apiResponse;
}
catch (Exception e)
{
if (log.IsErrorEnabled)
log.Error("Get failed", e);
throw new HttpUtilsException("Get failed", e);
}
}
示例8: GetOptions
// This function sends the OPTIONS request and returns an
// ASOptionsResponse class that represents the response.
public ASOptionsResponse GetOptions()
{
if (credential == null || server == null)
throw new InvalidDataException("ASOptionsRequest not initialized.");
string uriString = string.Format("{0}//{1}/Microsoft-Server-ActiveSync", UseSSL ? "https:" : "http:", Server);
Uri serverUri = new Uri(uriString);
CredentialCache creds = new CredentialCache();
// Using Basic authentication
creds.Add(serverUri, "Basic", Credentials);
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(uriString);
httpReq.Credentials = creds;
httpReq.Method = "OPTIONS";
try
{
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
ASOptionsResponse response = new ASOptionsResponse(httpResp);
httpResp.Close();
return response;
}
catch (Exception ex)
{
ASError.ReportException(ex);
return null;
}
}
示例9: SetupProxy
private static void SetupProxy()
{
// if a proxyURL is specified in the configuration file,
// create a WebProxy object for later use.
string proxyURL = AppSettings.GetSetting(null, PROXY_URL);
if (proxyURL != null)
{
mProxy = new WebProxy(proxyURL);
// if a proxyUser is specified in the configuration file,
// set up the proxy object's Credentials.
string proxyUser
= AppSettings.GetSetting(null, PROXY_USER);
if (proxyUser != null)
{
string proxyPassword
= AppSettings.GetSetting(null, PROXY_PASSWORD);
NetworkCredential credential
= new NetworkCredential(proxyUser, proxyPassword);
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(proxyURL), BASIC_AUTH, credential);
mProxy.Credentials = cache;
}
}
}
示例10: Main
static void Main(string[] args)
{
string strBaseUrl = "http://192.168.1.1";
string strUrl = strBaseUrl + "/userRpm/SysRebootRpm.htm?Reboot={0}";//%D6%D8%C6%F4%C2%B7%D3%C9%C6%F7";
string strCommand = "重启路由器";
strCommand = HttpUtility.UrlEncode(strCommand, Encoding.GetEncoding("GB2312"));
strUrl = string.Format(strUrl, strCommand);
NetworkCredential myCred = new NetworkCredential(
"1802", "1qaz2wsx",null);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(strBaseUrl), "Basic", myCred);
//myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
request.Credentials = myCache;
try
{
WebResponse response = request.GetResponse();
}
catch (Exception)
{
throw;
}
//string strIPCtlFormat = "QoSCtrl={0}&h_lineType={1}&h_down_bandWidth={2}";
//string strFormFormat = "QoSCtrl={0}&h_lineType={1}&h_down_bandWidth=4000&hips_1=1&hipe_1=2&hm_1=0&hbW_1=999&hn_1=hahaha&hen_1=1&hips_2=&hipe_2=&hm_2=0&hbW_2=&hn_2=&hen_2=&hips_3=&hipe_3=&hm_3=0&hbW_3=&hn_3=&hen_3=&hips_4=&hipe_4=&hm_4=0&hbW_4=&hn_4=&hen_4=&hips_5=&hipe_5=&hm_5=0&hbW_5=&hn_5=&hen_5=&hips_6=&hipe_6=&hm_6=0&hbW_6=&hn_6=&hen_6=&hips_7=&hipe_7=&hm_7=0&hbW_7=&hn_7=&hen_7=&hips_8=&hipe_8=&hm_8=0&hbW_8=&hn_8=&hen_8=&sv=%B1%A3%20%B4%E6"
}
示例11: reset
/// <summary>
/// 重启路由
/// </summary>
private string reset(string url, string userName, string password, string method, string data)
{
CookieContainer container = new CookieContainer();
string requestUriString = url;
if (method == "GET") requestUriString += "?" + data;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUriString);
request.Method = method;
if (method == "POST") {
byte[] POSTData = new UTF8Encoding().GetBytes(data);
request.ContentLength = POSTData.Length;
request.GetRequestStream().Write(POSTData, 0, POSTData.Length);
}
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;CIBA)";
request.CookieContainer = container;
request.KeepAlive = true;
request.Accept = "*/*";
request.Timeout = 3000;
request.PreAuthenticate = true;
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(requestUriString), "Basic", new NetworkCredential(routeInfo.UserName, routeInfo.RPassword));
request.Credentials = cache;
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = container.GetCookies(request.RequestUri);
new StreamReader(response.GetResponseStream(), Encoding.Default).Close();
response.Close();
return string.Empty;
} catch (Exception ex) {
return ex.Message;
}
}
示例12: TryGetCredentialAndProxy
private HttpMessageHandler TryGetCredentialAndProxy(PackageSource packageSource)
{
Uri uri = new Uri(packageSource.Source);
var proxy = ProxyCache.Instance.GetProxy(uri);
var credential = CredentialStore.Instance.GetCredentials(uri);
if (proxy != null && proxy.Credentials == null) {
proxy.Credentials = CredentialCache.DefaultCredentials;
}
if (credential == null && !String.IsNullOrEmpty(packageSource.UserName) && !String.IsNullOrEmpty(packageSource.Password))
{
var cache = new CredentialCache();
foreach (var scheme in _authenticationSchemes)
{
cache.Add(uri, scheme, new NetworkCredential(packageSource.UserName, packageSource.Password));
}
credential = cache;
}
if (proxy == null && credential == null) return null;
else
{
if(proxy != null) ProxyCache.Instance.Add(proxy);
if (credential != null) CredentialStore.Instance.Add(uri, credential);
return new WebRequestHandler()
{
Proxy = proxy,
Credentials = credential
};
}
}
示例13: AsCredentialCache
internal static ICredentials AsCredentialCache(this ICredentials credentials, Uri uri)
{
// No credentials then bail
if (credentials == null)
{
return null;
}
// Do nothing with default credentials
if (credentials == CredentialCache.DefaultCredentials ||
credentials == CredentialCache.DefaultNetworkCredentials)
{
return credentials;
}
// If this isn't a NetworkCredential then leave it alove
var networkCredentials = credentials as NetworkCredential;
if (networkCredentials == null)
{
return credentials;
}
// Set this up for each authentication scheme we support
// The reason we're using a credential cache is so that the HttpWebRequest will forward our
// credentials if there happened to be any redirects in the chain of requests.
var cache = new CredentialCache();
foreach (string scheme in _authenticationSchemes)
{
cache.Add(uri, scheme, networkCredentials);
}
return cache;
}
示例14: GetCredential
// Authentication using a Credential Cache to store the authentication
CredentialCache GetCredential(string uri, string username, string password)
{
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
var credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(uri), "Basic", new NetworkCredential(username, password));
return credentialCache;
}
示例15: btnGo_Click
private void btnGo_Click(object sender, EventArgs e)
{
string URL = string.Format("http://{1}/sdata/slx/dynamic/-/{0}?format=json",
"Accounts", "localhost:3333");
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(URL), "Basic",
new NetworkCredential("Admin", "[email protected]@kers123"));
WebRequest wreq = System.Net.WebRequest.Create(URL);
wreq.Credentials = myCache;
WebResponse wresp = wreq.GetResponse();
StreamReader sr = new StreamReader(wresp.GetResponseStream());
string jsonresp = sr.ReadToEnd();
AccountsMyWayJSON.Account j = JsonConvert.DeserializeObject<AccountsMyWayJSON.Account>(jsonresp);
int total = 0;
foreach (Dictionary<string,object> item in j.resources)
{
if ((item["Employees"]!=null))
{
total += int.Parse(item["Employees"].ToString());
}
}
j.totalEmployeesinResults = total;
List<Dictionary<string,object>> r = new List<Dictionary<string,object>>(0);
j.resources = r;
rtbDisplay.Text = JsonConvert.SerializeObject(j);
}