5

Sto costruendo un'app di Windows Store utilizzando Windows Runtime. Sto accedendo a un servizio OData che utilizza l'autenticazione di base. Sto usando la libreria WCF Data Services Tools for Windows Store Apps (Microsoft.Data.Services.Client.WindowsStore).Come si aggiunge un'intestazione di autorizzazione personalizzata a un client OData di Windows Store App?

La stringa di autenticazione è un formato personalizzato, quindi non è possibile utilizzare solo uno NetworkCredential(username, password). Ho bisogno di aggiungere l'intestazione me stesso ad ogni richiesta dal mio DataServiceContext.

Ho provato ad utilizzare il seguente codice:

proxy.SendingRequest += (s, e) => 
{ 
    e.RequestHeaders.Add("Authorization", authHeader); 
} 

Ma ricevo l'errore:

'System.Net.WebHeaderCollection' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Net.WebHeaderCollection' could be found 

risposta

14

È possibile utilizzare la nuova SendingRequest2 caso in cui gli incendi, dopo la richiesta è costruito e prima che venga inviato al server.

C'è un metodo RequestMessage.SetHeader(headername, value) che è possibile utilizzare per impostare le intestazioni. Impostare il valore su null per rimuovere un'intestazione.

proxy.SendingRequest2 += (sender, eventArgs) => 
{ 
    eventArgs.RequestMessage.SetHeader("Authorization", authHeader); 
}; 

I WCF Data Services team blog parla più su di esso:

SendingRequest2 (and its deprecated predecessor SendingRequest) fires after the request is built. WebRequest does not allow you to modify the URL after construction. The new event lets you modify the URL before we build the underlying request, giving you full control over the request.

+1

si chiesto e ha risposto alla tua domanda in breve tempo. qual e il punto? – lontivero

+9

@lontivero per rendere Internet migliore http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ –

Problemi correlati