栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

Asp.net cookie的处理流程深入分析

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Asp.net cookie的处理流程深入分析

一说到cookie我想大家都应该知道它是一个保存在客户端,当浏览器请求一个url时,浏览器会携带相关的cookie达到服务器端,所以服务器是可以操作cookie的,在Response时,会把cookie信息输出到客服端。下面我们来看一个demo吧,代码如下:

第一次请求结果如下:

第二次请求结果如下:

到这里我们可以看到第二次请求传入的cookie正好是第一次请求返回的cookie信息,这里的cookie信息的维护主要是我们客户端的浏览器,但是在Asp.net程序开发时,cookie往往是在服务端程序里面写入,就如我的事例代码;很少有用客服端js实现的。现在我们就来看看asp.net服务端是如何实现读写cookie的。

首先我们来看看HttpRequest的cookie是如何定义的:
复制代码 代码如下:
public HttpcookieCollection cookies {
get {
Ensurecookies();
if (_flags[needToValidatecookies]) {
_flags.Clear(needToValidatecookies);
ValidatecookieCollection(_cookies);
}
return _cookies;
}
}

这里的cookie获取主要是调用一个Ensurecookies方法,Ensurecookies放主要是调用
复制代码 代码如下:
// Populates the cookies property but does not hook up validation.
internal HttpcookieCollection Ensurecookies() {
if (_cookies == null) {
_cookies = new HttpcookieCollection(null, false);
if (_wr != null)
FillIncookiesCollection(_cookies, true );

if (HasTransitionedToWebSocketRequest) // cookies can't be modified after the WebSocket handshake is complete
_cookies.MakeReadonly();
}
return _cookies;
}

public sealed class HttpcookieCollection : NameObjectCollectionbase
{
internal HttpcookieCollection(HttpResponse response, bool readOnly) : base(StringComparer.OrdinalIgnoreCase)
{
this._response = response;
base.IsReadonly = readOnly;
}
}

其中这里的FillIncookiesCollection方法实现也比较复杂:
复制代码 代码如下:
internal void FillIncookiesCollection(HttpcookieCollection cookieCollection, bool includeResponse) {
if (_wr == null)
return;

String s = _wr.GetKnownRequestHeader(HttpWorkerRequest.Headercookie);

// Parse the cookie server variable.
// Format: c1=k1=v1&k2=v2; c2=...

int l = (s != null) ? s.Length : 0;
int i = 0;
int j;
char ch;

Httpcookie lastcookie = null;

while (i < l) {
// find next ';' (don't look to ',' as per 91884)
j = i;
while (j < l) {
ch = s[j];
if (ch == ';')
break;
j++;
}

// create cookie form string
String cookieString = s.Substring(i, j-i).Trim();
i = j+1; // next cookie start

if (cookieString.Length == 0)
continue;

Httpcookie cookie = CreatecookieFromString(cookieString);

// some cookies starting with '$' are really attributes of the last cookie
if (lastcookie != null) {
String name = cookie.Name;

// add known attribute to the last cookie (if any)
if (name != null && name.Length > 0 && name[0] == '$') {
if (StringUtil.EqualsIgnoreCase(name, "$Path"))
lastcookie.Path = cookie.Value;
else if (StringUtil.EqualsIgnoreCase(name, "$Domain"))
lastcookie.Domain = cookie.Value;

continue;
}
}

// regular cookie
cookieCollection.Addcookie(cookie, true);
lastcookie = cookie;

// goto next cookie
}

// Append response cookies
if (includeResponse) {
// If we have a reference to the response cookies collection, use it directly
// rather than going through the Response object (which might not be available, e.g.
// if we have already transitioned to a WebSockets request).
HttpcookieCollection storedResponsecookies = _storedResponsecookies;
if (storedResponsecookies == null && !HasTransitionedToWebSocketRequest && Response != null) {
storedResponsecookies = Response.GetcookiesNoCreate();
}

if (storedResponsecookies != null && storedResponsecookies.Count > 0) {
Httpcookie[] responsecookieArray = new Httpcookie[storedResponsecookies.Count];
storedResponsecookies.CopyTo(responsecookieArray, 0);
for (int icookie = 0; icookie < responsecookieArray.Length; icookie++)
cookieCollection.Addcookie(responsecookieArray[icookie], append: true);
}

// release any stored reference to the response cookie collection
_storedResponsecookies = null;
}
}

说简单一点它主要调用HttpWorkerRequest的GetKnownRequestHeader方法获取浏览器传进来的cookie字符串信息,然后再把这些信息根据;来分隔成多个Httpcookie实例。把这些Httpcookie实例添加到传进来的HttpcookieCollection参数。

这里HttpWorkerRequest继承结果如下:
复制代码 代码如下:
internal class ISAPIWorkerRequestInProcForIIS7 : ISAPIWorkerRequestInProcForIIS6
internal class ISAPIWorkerRequestInProcForIIS6 : ISAPIWorkerRequestInProc
internal class ISAPIWorkerRequestInProc : ISAPIWorkerRequest
internal abstract class ISAPIWorkerRequest : HttpWorkerRequest

其中 GetKnownRequestHeader方法的实现主要是在ISAPIWorkerRequest中,其GetKnownRequestHeader主要是调用了它的ReadRequestHeaders私有方法,在ReadRequestHeaders方法中主要是调用它的this.GetServerVariable("ALL_RAW")方法,所以我们可以认为this.GetServerVariable("ALL_RAW")这个方法是获取客户端传来的cookie参数,而GetServerVariable方法的实现主要是在ISAPIWorkerRequestInProc 类,具体实现非常复杂。

这里的GetKnownRequestHeader方法实现非常复杂我们也就不去深研它了,我们只要知道调用这个方法就会返回cookie的所有字符串信息。在这个方法里面还调用了一个CreatecookieFromString方法,根据字符串来创建我们的Httpcookie实例。CreatecookieFromString方法实现如下:
复制代码 代码如下:
internal static Httpcookie CreatecookieFromString(String s) {
Httpcookie c = new Httpcookie();

int l = (s != null) ? s.Length : 0;
int i = 0;
int ai, ei;
bool firstValue = true;
int numValues = 1;

// Format: cookiename[=key1=val2&key2=val2&...]

while (i < l) {
// find next &
ai = s.IndexOf('&', i);
if (ai < 0)
ai = l;

// first value might contain cookie name before =
if (firstValue) {
ei = s.IndexOf('=', i);

if (ei >= 0 && ei < ai) {
c.Name = s.Substring(i, ei-i);
i = ei+1;
}
else if (ai == l) {
// the whole cookie is just a name
c.Name = s;
break;
}

firstValue = false;
}

// find '='
ei = s.IndexOf('=', i);

if (ei < 0 && ai == l && numValues == 0) {
// simple cookie with simple value
c.Value = s.Substring(i, l-i);
}
else if (ei >= 0 && ei < ai) {
// key=value
c.Values.Add(s.Substring(i, ei-i), s.Substring(ei+1, ai-ei-1));
numValues++;
}
else {
// value without key
c.Values.Add(null, s.Substring(i, ai-i));
numValues++;
}

i = ai+1;
}

return c;
}

我们平时很少用到Httpcookie的Values属性,所以这个属性大家还是需要注意一下,这个方法就是把一个cookie的字符串转化为相应的Httpcookie实例。
现在我们回到HttpRequest的cookies属性中来,这里有一个关于cookie的简单验证
复制代码 代码如下:
private void ValidatecookieCollection(HttpcookieCollection cc) {
if (_enableGranularValidation) {
// Granular request validation is enabled - validate collection entries only as they're accessed.
cc.EnableGranularValidation((key, value) => ValidateString(value, key, RequestValidationSource.cookies));
}
else {
// Granular request validation is disabled - eagerly validate all collection entries.
int c = cc.Count;

for (int i = 0; i < c; i++) {
String key = cc.GetKey(i);
String val = cc.Get(i).Value;

if (!String.IsNullOrEmpty(val))
ValidateString(val, key, RequestValidationSource.cookies);
}
}
}


其中HttpcookieCollection的EnableGranularValidation实现如下:
复制代码 代码如下:
internal void EnableGranularValidation(ValidateStringCallback validationCallback)
{
this._keysAwaitingValidation = new HashSet(this.Keys.Cast(), StringComparer.OrdinalIgnoreCase);
this._validationCallback = validationCallback;
}

private void EnsureKeyValidated(string key, string value)
{
if ((this._keysAwaitingValidation != null) && this._keysAwaitingValidation.Contains(key))
{
if (!string.IsNullOrEmpty(value))
{
this._validationCallback(key, value);
}
this._keysAwaitingValidation.Remove(key);
}
}



到这里我们知道默认从浏览器发送到服务器端的cookie都是需要经过次验证的。这里的ValidateString方法具体实现我们就不说了,不过大家需要知道它是调用了RequestValidator.Current.IsValidRequestString方法来实现验证的,有关RequestValidator的信息大家可以查看HttpRequest的QueryString属性 的一点认识 。现在我们获取cookie已经基本完成了。那么我们接下来看看是如何添加cookie的了。

首先我们来看看HttpResponse的cookie属性:
复制代码 代码如下:
public HttpcookieCollection cookies
{
get
{
if (this._cookies == null)
{
this._cookies = new HttpcookieCollection(this, false);
}
return this._cookies;
}
}


接下来我们看看Httpcookie的实现如下:
复制代码 代码如下:
public sealed class Httpcookie {
private String _name;
private String _path = "/";
private bool _secure;
private bool _httpOnly;
private String _domain;
private bool _expirationSet;
private DateTime _expires;
private String _stringValue;
private HttpValueCollection _multiValue;
private bool _changed;
private bool _added;

internal Httpcookie() {
_changed = true;
}



///
///
/// Initializes a new instance of the
/// class.
///

///

public Httpcookie(String name) {
_name = name;

SetDefaultsFromConfig();
_changed = true;
}



///
///
/// Initializes a new instance of the
/// class.
///

///

public Httpcookie(String name, String value) {
_name = name;
_stringValue = value;

SetDefaultsFromConfig();
_changed = true;
}

private void SetDefaultsFromConfig() {
HttpcookiesSection config = RuntimeConfig.GetConfig().Httpcookies;
_secure = config.RequireSSL;
_httponly = config.HttpOnlycookies;

if (config.Domain != null && config.Domain.Length > 0)
_domain = config.Domain;
}


internal bool Changed {
get { return _changed; }
set { _changed = value; }
}


internal bool Added {
get { return _added; }
set { _added = value; }
}

// DevID 251951 cookie is getting duplicated by ASP.NET when they are added via a native module
// This flag is used to remember that this cookie came from an IIS Set-Header flag,
// so we don't duplicate it and send it back to IIS
internal bool FromHeader {
get;
set;
}



///
///
/// Gets
/// or sets the name of cookie.
///

///

public String Name {
get { return _name;}
set {
_name = value;
_changed = true;
}
}



///
///
/// Gets or sets the URL prefix to transmit with the
/// current cookie.
///

///

public String Path {
get { return _path;}
set {
_path = value;
_changed = true;
}
}



///
///
/// Indicates whether the cookie should be transmitted only over HTTPS.
///

///

public bool Secure {
get { return _secure;}
set {
_secure = value;
_changed = true;
}
}

///


/// Determines whether this cookie is allowed to participate in output caching.
///

///
/// If a given HttpResponse contains one or more outbound cookies with Shareable = false (the default value),
/// output caching will be suppressed for that response. This prevents cookies that contain potentially
/// sensitive information, e.g. FormsAuth cookies, from being cached in the response and sent to multiple
/// clients. If a developer wants to allow a response containing cookies to be cached, he should configure
/// caching as normal for the response, e.g. via the OutputCache directive, MVC's [OutputCache] attribute,
/// etc., and he should make sure that all outbound cookies are marked Shareable = true.
///

public bool Shareable {
get;
set; // don't need to set _changed flag since Set-cookie header isn't affected by value of Shareable
}

///
///
/// Indicates whether the cookie should have Httponly attribute
///

///

public bool Httponly {
get { return _httpOnly;}
set {
_httponly = value;
_changed = true;
}
}



///
///
/// Restricts domain cookie is to be used with.
///

///

public String Domain {
get { return _domain;}
set {
_domain = value;
_changed = true;
}
}



///
///
/// Expiration time for cookie (in minutes).
///

///

public DateTime Expires {
get {
return(_expirationSet ? _expires : DateTime.MinValue);
}

set {
_expires = value;
_expirationSet = true;
_changed = true;
}
}



///
///
/// Gets
/// or
/// sets an individual cookie value.
///

///

public String Value {
get {
if (_multiValue != null)
return _multiValue.ToString(false);
else
return _stringValue;
}

set {
if (_multiValue != null) {
// reset multivalue collection to contain
// single keyless value
_multiValue.Reset();
_multiValue.Add(null, value);
}
else {
// remember as string
_stringValue = value;
}
_changed = true;
}
}



///
/// Gets a
/// value indicating whether the cookie has sub-keys.

///

public bool HasKeys {
get { return Values.HasKeys();}
}

private bool SupportsHttponly(HttpContext context) {
if (context != null && context.Request != null) {
HttpBrowserCapabilities browser = context.Request.Browser;
return (browser != null && (browser.Type != "IE5" || browser.Platform != "MacPPC"));
}
return false;
}



///
/// Gets individual key:value pairs within a single cookie object.
///

public NamevalueCollection Values {
get {
if (_multiValue == null) {
// create collection on demand
_multiValue = new HttpValueCollection();

// convert existing string value into multivalue
if (_stringValue != null) {
if (_stringValue.IndexOf('&') >= 0 || _stringValue.IndexOf('=') >= 0)
_multiValue.FillFromString(_stringValue);
else
_multiValue.Add(null, _stringValue);

_stringValue = null;
}
}

_changed = true;

return _multiValue;
}
}



///
///
/// Shortcut for Httpcookie$Values[key]. Required for ASP compatibility.
///

///

public String this[String key]
{
get {
return Values[key];
}

set {
Values[key] = value;
_changed = true;
}
}


internal HttpResponseHeader GetSetcookieHeader(HttpContext context) {
StringBuilder s = new StringBuilder();

// cookiename=
if (!String.IsNullOrEmpty(_name)) {
s.Append(_name);
s.Append('=');
}

// key=value&...
if (_multiValue != null)
s.Append(_multiValue.ToString(false));
else if (_stringValue != null)
s.Append(_stringValue);

// domain
if (!String.IsNullOrEmpty(_domain)) {
s.Append("; domain=");
s.Append(_domain);
}

// expiration
if (_expirationSet && _expires != DateTime.MinValue) {
s.Append("; expires=");
s.Append(HttpUtility.FormatHttpcookieDateTime(_expires));
}

// path
if (!String.IsNullOrEmpty(_path)) {
s.Append("; path=");
s.Append(_path);
}

// secure
if (_secure)
s.Append("; secure");

// httponly, Note: IE5 on the Mac doesn't support this
if (_httponly && SupportsHttponly(context)) {
s.Append("; HttpOnly");
}

// return as HttpResponseHeader
return new HttpResponseHeader(HttpWorkerRequest.HeaderSetcookie, s.ToString());
}
}

现在我们回到HttpcookieCollection的Add方法看看,
复制代码 代码如下:
public void Add(Httpcookie cookie) {
if (_response != null)
_response.BeforecookieCollectionChange();

Addcookie(cookie, true);

if (_response != null)
_response.oncookieAdd(cookie);
}

public sealed class HttpResponse
{
internal void BeforecookieCollectionChange()
{
if (this._headersWritten)
{
throw new HttpException(SR.GetString("Cannot_modify_cookies_after_headers_sent"));
}
}
internal void oncookieAdd(Httpcookie cookie)
{
this.Request.AddResponsecookie(cookie);
}
}
public sealed class HttpRequest
{
internal void AddResponsecookie(Httpcookie cookie)
{
if (this._cookies != null)
{
this._cookies.Addcookie(cookie, true);
}
if (this._params != null)
{
this._params.MakeReadWrite();
this._params.Add(cookie.Name, cookie.Value);
this._params.MakeReadonly();
}
}
}


到这里我们应该知道每添加或修改一个cookie都会调用HttpResponse的BeforecookieCollectionChange和OncookieAdd方法,BeforecookieCollectionChange是确认我们的cookie是否可以添加的,以前在项目中就遇到这里的错误信息说什么“在header发送后不能修改cookie”,看见默认情况下_headersWritten是false,那么它通常在哪里被设置为true了,在HttpReaponse的BeginExecuteUrlForEntireResponse、Flush、EndFlush方法中被设置为true,而我们最常接触到的还是Flush方法。这里的OncookieAdd方法确保cookie实例同时也添加到HttpRequest中。
复制代码 代码如下:
internal void Addcookie(Httpcookie cookie, bool append) {
ThrowIfMaxHttpCollectionKeysExceeded();

_all = null;
_allKeys = null;

if (append) {
// DevID 251951 cookie is getting duplicated by ASP.NET when they are added via a native module
// Need to not double add response cookies from native modules
if (!cookie.FromHeader) {
// mark cookie as new
cookie.Added = true;
}
baseAdd(cookie.Name, cookie);
}
else {
if (baseGet(cookie.Name) != null) {
// mark the cookie as changed because we are overriding the existing one
cookie.Changed = true;
}
baseSet(cookie.Name, cookie);
}
}
private void ThrowIfMaxHttpCollectionKeysExceeded() {
if (Count >= AppSettings.MaxHttpCollectionKeys) {
throw new InvalidOperationException(SR.GetString(SR.CollectionCountExceeded_HttpValueCollection, AppSettings.MaxHttpCollectionKeys));
}
}


这里的Addcookie方法也非常简单,不过每次添加都会去检查cookie的个数是否超过最大值。其实添加cookie还可以调用HttpResponse的Appendcookie方法,
复制代码 代码如下:
public void Appendcookie(Httpcookie cookie)
{
if (this._headersWritten)
{
throw new HttpException(SR.GetString("Cannot_append_cookie_after_headers_sent"));
}
this.cookies.Addcookie(cookie, true);
this.oncookieAdd(cookie);
}


这里它的实现和HttpcookieCollection的     public void Add(Httpcookie cookie)方法实现一致。
 同样我们也知道这些cookie是在HttpResponse的GenerateResponseHeadersForcookies方法中被使用,
其中GenerateResponseHeadersForcookies方法的实现如下:
复制代码 代码如下:
internal void GenerateResponseHeadersForcookies()
{
if (_cookies == null || (_cookies.Count == 0 && !_cookies.Changed))
return; // no cookies exist

HttpHeaderCollection headers = Headers as HttpHeaderCollection;
HttpResponseHeader cookieHeader = null;
Httpcookie cookie = null;
bool needToReset = false;

// Go through all cookies, and check whether any have been added
// or changed. If a cookie was added, we can simply generate a new
// set cookie header for it. If the cookie collection has been
// changed (cleared or cookies removed), or an existing cookie was
// changed, we have to regenerate all Set-cookie headers due to an IIS
// limitation that prevents us from being able to delete specific
// Set-cookie headers for items that changed.
if (!_cookies.Changed)
{
for(int c = 0; c < _cookies.Count; c++)
{
cookie = _cookies[c];
if (cookie.Added) {
// if a cookie was added, we generate a Set-cookie header for it
cookieHeader = cookie.GetSetcookieHeader(_context);
headers.SetHeader(cookieHeader.Name, cookieHeader.Value, false);
cookie.Added = false;
cookie.Changed = false;
}
else if (cookie.Changed) {
// if a cookie has changed, we need to clear all cookie
// headers and re-write them all since we cant delete
// specific existing cookies
needToReset = true;
break;
}
}
}


if (_cookies.Changed || needToReset)
{
// delete all set cookie headers
headers.Remove("Set-cookie");

// write all the cookies again
for(int c = 0; c < _cookies.Count; c++)
{
// generate a Set-cookie header for each cookie
cookie = _cookies[c];
cookieHeader = cookie.GetSetcookieHeader(_context);
headers.SetHeader(cookieHeader.Name, cookieHeader.Value, false);
cookie.Added = false;
cookie.Changed = false;
}

_cookies.Changed = false;
}
}

这里我们还是来总结一下吧:在HttpWorkerRequest中我们调用 GetKnownRequestHeader方法来获取cookie的字符串形式,然后再将这里的字符串转化为Httpcookie集合供 HttpRequest使用,在HttpResponse中的GenerateResponseHeadersForcookies方法中会处理我们的 cookie实例,调用cookie的GetSetcookieHeader方法得到Httpcookie对应的字符串值,然后把该值添加到 HttpHeaderCollection 集合中(或者修改已有的值)。在获取cookie是这里有一个验证需要我们注意的就是 RequestValidator.Current.IsValidRequestString方法。   在添加或修改cookie是有2个地方的检查(1)检查cookie的个数是否达到我们配置的cookie最大个数,(2)现在是否已经写入头信息,如果 头信息已经写了则不能操作cookie。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/58222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号