Ich hatte ein Problem, mehrere Cookies im ISAPI-Filter festzulegen. Ich möchte die HttpOnly
Flagge in allen Cookies hinzufügen.So legen Sie mehrere Cookies im ISAPI-Filter fest
Also, in meinem ersten Versuch, teile ich die Cookies Wert und fügen Sie die HttpOnly
Flag, dann kombiniere ich sie in eine Zeichenfolge, aufrufen am Ende, der Browser erhalten nur erste Cookie-Wert.
Code of 1. Versuch:
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:","");
}
Im zweiten Versuch, spaltete ich die den Cookie-Wert, und rufen Sie pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue)
für jedes Cookie, aber der Browser nur das letzte Cookie in diesem Fall erhalten.
Code of 2. Versuch:
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);
}
Ich tue dies in IE10 + Win2008 R2. In beiden Fällen haben die Ergebnis-Cookie-Strings das korrekte Format. Hat jemand eine Ahnung davon?
Dieses Problem besteht im Grunde, weil Sie beim Aufruf von GetHeader
alle Cookies in einer durch Kommas getrennten Zeichenfolge erhalten. Was wäre die beste Methode, um die SetHeader
Methode zu verwenden, um alle Cookies auf die Antwort zurückzusetzen?
Sie sollten '_countof (lszNewBuffer) verwenden' statt 'sizeof (lszNewBuffer)' mit 'strcpy_s' und' strcat_s' wie es viele Zeichen muss und nicht Bytes. Hier funktioniert es, weil Boths gleich sind wie Strings ANSI sind. – McX