5

मैं विंडोज रनटाइम का उपयोग कर एक विंडोज स्टोर ऐप का निर्माण कर रहा हूं। मैं एक ओडीटा सेवा तक पहुंच रहा हूं जो मूल प्रमाणीकरण का उपयोग करता है। मैं WCF Data Services Tools for Windows Store Apps लाइब्रेरी (Microsoft.Data.Services.Client.WindowsStore) का उपयोग कर रहा हूं।आप Windows Store App OData क्लाइंट में कस्टम प्राधिकरण शीर्षलेख कैसे जोड़ते हैं?

प्रमाणीकरण स्ट्रिंग एक कस्टम प्रारूप है, इसलिए मैं केवल NetworkCredential(username, password) का उपयोग नहीं कर सकता। मुझे अपने DataServiceContext से प्रत्येक अनुरोध में हेडर जोड़ना होगा।

मैं निम्नलिखित कोड का उपयोग कर की कोशिश की:

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

लेकिन मैं त्रुटि प्राप्त:

'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 

उत्तर

14

आप और इससे पहले कि यह भेज दिया जाता है नई SendingRequest2 घटना है कि अनुरोध के बाद आग बनाया गया है का उपयोग कर सकते सर्वर के लिए।

RequestMessage.SetHeader(headername, value) विधि है जिसका उपयोग आप हेडर सेट करने के लिए कर सकते हैं। हेडर को हटाने के लिए मान को null पर सेट करें।

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

WCF Data Services team blog वार्ता इसके बारे में अधिक:

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

के लिए कहा और कम समय में अपने खुद के सवाल का जवाब दे। क्या बात है? – lontivero

+9

@lontivero इंटरनेट को बेहतर बनाने के लिए http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ –