Wystąpił problem z ustawieniem wielu plików cookie w filtrze ISAPI. Chcę dodać flagę HttpOnly
do wszystkich plików cookie.Jak ustawić wiele plików cookie w filtrze ISAPI
W pierwszej próbie podzielę wartość plików cookie i dodaję flagę HttpOnly
, a następnie łączę je w jeden ciąg, w końcu wywołuję pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue)
, przeglądarka otrzymuje tylko pierwszą wartość cookie.
Kodeks 1. próba:
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:","");
}
W drugiej próbie, ja podzielić wartość ciasteczka, i powołać pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue)
dla każdego pliku cookie, ale przeglądarka uzyskać tylko ostatni cookie w tej sprawie.
Kodeks 2. podejściu:
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);
}
zrobić to w IE10 + Win2008 R2. W obu przypadkach wynikowy plik cookie ma prawidłowy format. Czy ktoś ma jakiekolwiek o tym pojęcie?
Ten problem występuje w zasadzie dlatego, że po wywołaniu GetHeader
wszystkie pliki cookie są wysyłane w ciągu rozdzielanym przecinkami. Jaki byłby najlepszy sposób użycia metody SetHeader
do ustawienia wszystkich plików cookie z powrotem na odpowiedź?
Powinieneś użyć '_countof (lszNewBuffer)' zamiast 'sizeof (lszNewBuffer)' z 'strcpy_s' i' strcat_s', ponieważ potrzebuje liczby znaków, a nie bajtów. Tutaj działa, ponieważ oba są równe łańcuchom ANSI. – McX