ho affrontato un problema per impostare più cookie nel filtro ISAPI. Voglio aggiungere il flag HttpOnly
in tutti i cookie.Come impostare più cookie nel filtro ISAPI
Così, nel mio primo tentativo, ho diviso il valore biscotti e aggiungere il flag HttpOnly
, poi li combino in un'unica stringa, invocare pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue)
alla fine, il browser ottiene solo il primo valore del cookie.
Codice del 1 ° tentativo:
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:","");
}
Nel secondo tentativo, ho diviso il valore del cookie, e invocare pResponse->SetHeader(pfc, "Set-Cookie:", szNewValue)
per ogni biscotto, ma il browser ottiene solo l'ultimo biscotto in questo caso.
Codice di 2 ° tentativo:
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);
}
mi fate questo in IE10 + Win2008 R2. In entrambi i casi, le stringhe dei cookie dei risultati sono nel formato corretto. Qualcuno ha qualche idea su questo?
Questo problema esiste fondamentalmente perché quando si richiama GetHeader
, si ricevono tutti i cookie in una stringa delimitata da una virgola. Quale sarebbe il modo migliore per utilizzare il metodo SetHeader
per riportare tutti i cookie alla risposta?
Si dovrebbe usare '_countof (lszNewBuffer)' invece di 'sizeof (lszNewBuffer)' con 'strcpy_s' e' strcat_s' in quanto ha bisogno del numero di caratteri e non di byte. Qui funziona perché entrambi sono uguali poiché le stringhe sono ANSI. – McX