本文整理汇总了C#中Cookie类的典型用法代码示例。如果您正苦于以下问题:C# Cookie类的具体用法?C# Cookie怎么用?C# Cookie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Cookie类属于命名空间,在下文中一共展示了Cookie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteCookie
/// <summary>
/// Delete a cookie in the browser by passing in a copy of a cookie
/// </summary>
/// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
public void DeleteCookie(Cookie cookie)
{
if (cookie != null)
{
this.DeleteCookieNamed(cookie.Name);
}
}
示例2: GetFormsCredentials
public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
{
// not use FormsCredentials unless you have implements a custom autentication.
authCookie = null;
user = password = authority = null;
return false;
}
示例3: GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch
public void GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie1 = new Cookie(CookieName1, CookieValue1);
var cookie2 = new Cookie(CookieName2, CookieValue2) { Version = 1 };
var uri = new Uri(SchemePrefix + OriginalDomain);
container.Add(uri, cookie1);
container.Add(uri, cookie2);
var cookies = container.GetCookies(uri);
Assert.Equal(2, cookies.Count);
Assert.Equal(OriginalDomain, cookies[CookieName1].Domain);
Assert.Equal(OriginalDomain, cookies[CookieName2].Domain);
uri = new Uri(SchemePrefix + "www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
}
示例4: GetFormsCredentials
public bool GetFormsCredentials(out Cookie authCookie, out string user,
out string password, out string authority)
{
authCookie = null;
user = password = authority = null;
return false;
}
示例5: AddAuthenticationCookies
private static void AddAuthenticationCookies(HttpContext context, System.Net.HttpWebRequest req)
{
// Copy user identity to new cookie
if (context.User != null && context.User.Identity != null)
{
var cookiesString0 = req.Headers[HttpRequestHeader.Cookie];
var cookies = CookieParser.Parse(cookiesString0).ToList();
// Pass username
var userCookie = new Cookie("") { Name = "username", Value = context.User.Identity.Name };
cookies.Add(userCookie);// Always add because this
// Pass user groups
var windowsIdentity = context.User.Identity as System.Security.Principal.WindowsIdentity;
var groupNames = new string[] { };
if (windowsIdentity.Groups != null)
groupNames = windowsIdentity.Groups.Select(g => g.Translate(typeof(System.Security.Principal.NTAccount)).Value).ToArray();
var groupsCookie = new Cookie("") { Name = "usergroups", Value = string.Join(",", groupNames) };
cookies.Add(groupsCookie);
// Authentication ticket
var ticket = TicketHandler.IssueTicket();
var ticketCookie = new Cookie("") { Name = "winauthticket", Value = ticket };
cookies.Add(ticketCookie);
var cookiesString = CookieParser.ToString(cookies.ToArray());
req.Headers[HttpRequestHeader.Cookie] = cookiesString;
}
}
示例6: GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains
public void GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie1 = new Cookie(CookieName1, CookieValue1) { Domain = OriginalDomain };
container.Add(new Uri(SchemePrefix + OriginalDomain), cookie1);
var uri = new Uri(SchemePrefix + OriginalDomain);
var cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
Assert.Equal(OriginalDomain, cookies[CookieName1].Domain);
uri = new Uri(SchemePrefix + "www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
}
示例7: Ctor_NameValuePath_Success
public static void Ctor_NameValuePath_Success()
{
Cookie c = new Cookie("hello", "goodbye", "foo");
Assert.Equal("hello", c.Name);
Assert.Equal("goodbye", c.Value);
Assert.Equal("foo", c.Path);
}
示例8: CookieJar_Example_Weighted
public void CookieJar_Example_Weighted(int total, int choc1, int choc2, int expectedNumerator, int expectedDenominator)
{
// Given we have 2 jars
// jar one contains 10 choc and 30 vanilla
// jar two contains 20 choc and 20 vanilla
// what is the probability of drawing a
// vanilla cookie from jar 1
var sample1 = new WeightedSample<Cookie>("Jar1");
var sample2 = new WeightedSample<Cookie>("Jar2");
var hypos = new HypoSet<Cookie>("All");
hypos.Add(sample1, sample2);
var chocx = new Cookie() { F = 'C' };
var vanix = new Cookie() { F = 'V' };
var choc = It.Is(chocx);
var vani = It.Is(vanix);
sample1[chocx] = choc1;
sample1[vanix] = total - choc1;
sample2[chocx] = choc2;
sample2[vanix] = total - choc2;
sample1.ProbabilityOfEvent(choc);
sample2.ProbabilityOfEvent(choc);
var postProb = hypos.PosterierProbability(sample1, vani);
Assert.That(postProb.Numerator, Is.EqualTo(expectedNumerator));
Assert.That(postProb.Denominator, Is.EqualTo(expectedDenominator));
}
示例9: HTTPWrapper
public HTTPWrapper(Cookie sharedCookie)
{
UseGZip = true;
UseProxy = false;
lastReq = new LastRequest();
cookies = sharedCookie;
retries = DEFAULT_RETRIES;
}
示例10: CookiePortTest
public CookiePortTest()
{
_cc = new CookieContainer();
_cookie = new Cookie("name", "value1", "/path", "localhost");
// use both space and comma as delimiter
_cookie.Port = "\"80 110,1050, 1090 ,1100\"";
_cc.Add(new Uri("http://localhost/path"), _cookie);
}
示例11: CommentUri_GetSet_Success
public static void CommentUri_GetSet_Success()
{
Cookie c = new Cookie();
Assert.Null(c.CommentUri);
c.CommentUri = new Uri("http://hello.com");
Assert.Equal(new Uri("http://hello.com"), c.CommentUri);
}
示例12: ShouldAddCookie
public void ShouldAddCookie()
{
driver.Get(macbethPage);
Cookie cookie = new Cookie("cookie", "monster");
driver.Manage().AddCookie(cookie);
Assert.That(driver.Manage().GetCookies().ContainsKey(cookie.Name),
"Cookie was not added successfully");
}
示例13: OnAuthRequestFinished
void OnAuthRequestFinished(HTTPRequest req, HTTPResponse resp)
{
AuthRequest = null;
string failReason = string.Empty;
switch (req.State)
{
// The request finished without any problem.
case HTTPRequestStates.Finished:
if (resp.IsSuccess)
{
Cookie = resp.Cookies != null ? resp.Cookies.Find(c => c.Name.Equals(".ASPXAUTH")) : null;
if (Cookie != null)
{
HTTPManager.Logger.Information("CookieAuthentication", "Auth. Cookie found!");
if (OnAuthenticationSucceded != null)
OnAuthenticationSucceded(this);
// return now, all other paths are authentication failures
return;
}
else
HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Auth. Cookie NOT found!");
}
else
HTTPManager.Logger.Warning("CookieAuthentication", failReason = string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
resp.StatusCode,
resp.Message,
resp.DataAsText));
break;
// The request finished with an unexpected error. The request's Exception property may contain more info about the error.
case HTTPRequestStates.Error:
HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
break;
// The request aborted, initiated by the user.
case HTTPRequestStates.Aborted:
HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Aborted!");
break;
// Ceonnecting to the server is timed out.
case HTTPRequestStates.ConnectionTimedOut:
HTTPManager.Logger.Error("CookieAuthentication", failReason = "Connection Timed Out!");
break;
// The request didn't finished in the given time.
case HTTPRequestStates.TimedOut:
HTTPManager.Logger.Error("CookieAuthentication", failReason = "Processing the request Timed Out!");
break;
}
if (OnAuthenticationFailed != null)
OnAuthenticationFailed(this, failReason);
}
示例14: HttpResponseState
public HttpResponseState(HttpStatusCode statusCode, string statusDescription, Uri requestUrl, Header[] headers, Cookie[] cookies, string contentType)
{
StatusCode = statusCode;
StatusDescription = statusDescription;
RequestUrl = requestUrl;
Headers = headers;
Cookies = cookies;
ContentType = contentType;
}
示例15: Save
public void Save(Cookie cookie)
{
if (Response.Cookies.AllKeys.Contains(cookie.Name)) {
// Class was not designed to handle multiple Saves()
// Need it? _You_ implement it!
throw new Exception("Cookie has already been saved this session.");
}
Response.Cookies.Add(cookie.HttpCookie());
}