我在使用ISAPI篩選器設置多個Cookie時遇到問題。我想將HttpOnly
標誌添加到所有Cookie中。如何在ISAPI篩選器中設置多個Cookie
因此,在我第一次嘗試時,我分割了cookie值並添加了HttpOnly
標誌,然後我將它們合併爲一個字符串,最後調用pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue)
,瀏覽器只獲取第一個cookie值。
碼1的嘗試:
cbValue = sizeof(szValue)/sizeof(szValue[0]);
if (pResponse->GetHeader(pfc, "Set-Cookie:", szValue, &cbValue))
{
char szNewValue[MAX_URI_SIZE] = "";
char* token = NULL;
char* context = NULL;
char delim[] = ",";
// szValue format like
// "Language=en; expires=Sat, 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly,Language=en; expires=Sat, 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly"
// After first split
// token = "Language=en; expires=Sat"
// context = " 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly,Language=en; expires=Sat, 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly"
token = strtok_s(szValue, delim, &context);
while (token != NULL)
{
strcat_s(szNewValue, token);
if (NULL != context)
{
if (' ' != context[0] && !strstr(token, "HttpOnly"))
{
strcat_s(szNewValue, "; HttpOnly");
}
// context[0] = ' ' means it split the one whole cookie, not an entire cookie, we need append ","
// context[0] != '\0' means other cookies after, we need append delimiter ","
if (' ' == context[0] || '\0' != context[0])
{
strcat_s(szNewValue, ",");
}
}
// NULL, function just re-uses the context after the first read.
token = strtok_s(NULL, delim, &context);
}
if (!pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue))
{
// Fail securely - send no cookie!
pResponse->SetHeader(pfc,"Set-Cookie:","");
}
在第二次嘗試,我分裂cookie值,並調用pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue)
每一個cookie的,但瀏覽器只得到在這種情況下的最後一塊餅乾。
守則第2次嘗試的:
cbValue = sizeof(szValue)/sizeof(szValue[0]);
if (pResponse->GetHeader(pfc, "Set-Cookie:", szValue, &cbValue))
{
char szNewValue[MAX_URI_SIZE] = "";
char* token = NULL;
char* context = NULL;
char delim[] = ",";
// szValue format like
// "Language=en; expires=Sat, 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly,Language=en; expires=Sat, 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly"
// After first split
// token = "Language=en; expires=Sat"
// context = " 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly,Language=en; expires=Sat, 15-Jul-2113 02:46:27 GMT; path=/; HttpOnly"
token = strtok_s(szValue, delim, &context);
while (token != NULL)
{
strcat_s(szNewValue, token);
if (NULL != context)
{
if (' ' != context[0] && !strstr(token, "HttpOnly"))
{
strcat_s(szNewValue, "; HttpOnly");
}
// context[0] = ' ' means it split the one whole cookie, not an entire cookie, we need append ","
// context[0] != '\0' means other cookies after, we need append delimiter ","
if (' ' == context[0])// || '\0' != context[0])
{
strcat_s(szNewValue, ",");
}
if (' ' != context[0])
{
pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue);
strcpy(szNewValue, "");
}
}
// NULL, function just re-uses the context after the first read.
token = strtok_s(NULL, delim, &context);
}
我這樣做是IE10 +的Win2008 R2。在這兩種情況下,結果cookie字符串的格式都是正確的。有沒有人對此有任何線索?
,因爲當你調用GetHeader
,您會收到一個逗號分隔字符串的所有cookies,這些問題主要存在。使用SetHeader
方法將所有Cookie設置迴響應的最佳方式是什麼?
你應該使用'_countof(lszNewBuffer)'而不是'sizeof(lszNewBuffer)''strcpy_s'和'strcat_s',因爲它需要字符數而不是字節。這裏它的工作原理是因爲兩個字符串都是ANSI都是相等的。 – McX