How to retrieve the security descriptor of a message by using the HttpWebRequest class and the HttpWebResponse class in Visual C# (313127)



The information in this article applies to:

  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Exchange 2000 Server

This article was previously published under Q313127

SUMMARY

This article describes how to use Microsoft Visual C# to retrieve the http://schemas.microsoft.com/exchange/security/descriptorproperty of an object in Microsoft Exchange 2000 Server. Specifically, you use the HttpWebRequest class with the PROFIND method and the HttpWebResponse class from the System.Net namespace.

MORE INFORMATION

To retrieve the http://schemas.microsoft.com/exchange/security/descriptor property of an object, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C# Projects.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  4. Under Templates, click Console Application, and then click OK.

    In Visual Studio .NET, Class1.cs is created by default. In Visual Studio 2005, Program.cs is created by default.
  5. In the code window, replace the existing code with the following:
    using System;
    using System.Net;
    using System.IO;   
    
    namespace WebDavNET
    {
     /// <summary>
     /// Summary description for Class1.
     /// </summary>
     class Class1
     {
      static void Main(string[] args)
      {
       try 
       {
        // TODO: Replace with the URL of an object on Exchange 2000.
        string sUri = "http://ExchServer/Public/MyFolder/Test.EML";
    
        System.Uri myUri = new System.Uri(sUri);
        HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);
    
        string strSDType = "descriptor";
        string sQuery;
        sQuery = "<?xml version='1.0' encoding='utf-8'?>" + 
         "<propfind xmlns=\"DAV:\">" + 
         "<prop xmlns:r=\"http://schemas.microsoft.com/exchange/security/\">" + 
         "<isfolder/>" + 
         "   <r:" + strSDType + "/>" + 
         "</prop>" + 
         "</propfind>";
    
        // Set credentials.
        // TODO: Replace with appropriate user credentials.
        NetworkCredential myCred = new NetworkCredential(@"DomainName\UserName", "Password");
        CredentialCache myCredentialCache = new CredentialCache();
        myCredentialCache.Add(myUri, "Basic", myCred);
        HttpWRequest.Credentials = myCredentialCache;
    
        //Uncomment the following statement and comment the previous four statements if you 
        //use Integrated Windows authentication.
        //httpWRequest.Credentials = CredentialCache.DefaultCredentials
    
        //Note: In Basic type authentication, the user name and password are sent as base64-encoded text and are
        //easily decoded. To help protect the user name and password, we recommend that you
        //use SSL when you use Basic authentication.
    
        // Set headers.
        HttpWRequest.KeepAlive = false;
        HttpWRequest.Headers.Set("Pragma", "no-cache");
        HttpWRequest.ContentType =  "text/xml";
        HttpWRequest.Headers.Set("Translate", "f");
        HttpWRequest.Headers.Set("Depth", "0");
        HttpWRequest.ContentLength = sQuery.Length;
    
        //Set the request timeout to five minutes.
        HttpWRequest.Timeout = 300000;
        // set the request method
        HttpWRequest.Method = "PROPFIND";
    
        // Store the data in a byte array.
        byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
        HttpWRequest.ContentLength = ByteQuery.Length;
        Stream QueryStream = HttpWRequest.GetRequestStream();
        // write the data to be posted to the Request Stream
        QueryStream.Write(ByteQuery,0,ByteQuery.Length);
        QueryStream.Close();
    
        // Send the request and get a response. 
        HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
        
        // Get the status code.
        int iStatCode =  (int)HttpWResponse.StatusCode;
        string sStatus = iStatCode.ToString();
        Console.WriteLine("Status Code: {0}", sStatus);
        // Get the request headers
        string sReqHeaders = HttpWRequest.Headers.ToString();
        Console.WriteLine(sReqHeaders);
    
        // Read the response stream.
        Stream strm = HttpWResponse.GetResponseStream();
        StreamReader sr = new StreamReader(strm);
        string sText = sr.ReadToEnd();
        Console.WriteLine("Response: {0}", sText);
    
        // Close the stream.
        strm.Close();
    
        // Clean up.
        myCred = null;
        myCredentialCache = null;
        HttpWRequest = null;
        HttpWResponse = null;
        QueryStream = null;
        strm = null;
        sr = null;
           
       }
       catch (Exception e)
       {
        Console.WriteLine("{0} Exception caught.", e);
       }
      }
     }
    }
  6. Search for "TODO" in the code, and then modify the code for your environment.
  7. Press F5 to build and to run the program.
  8. Make sure that the security descriptor property for the object has been received from the response stream.

Modification Type:MajorLast Reviewed:1/20/2006
Keywords:kbMsg kbXML kbcode kbhowto KB313127 kbAudDeveloper