Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Exchange
2000.active.directory
2000.admin
2000.announcements
2000.app.conversion
2000.applications
2000.clients
2000.clustering
2000.connectivity
2000.development
2000.documentation
2000.general
2000.information.store
2000.interop
2000.kms
2000.misc
2000.protocols
2000.realtime.collabo.
2000.setup
2000.transport
2000.win2000
admin
application.conversion
applications
clients
clustering
connectivity
design
development
misc
mobility
setup
tools
  
 
date: Mon, 26 Feb 2007 08:52:10 +1100,    group: microsoft.public.exchange2000.development        back       


Could not establish trust relationship for the SSL/TLS secure channel.   
Hi

When i run the code below I get the follwing error:

The underlying connection was closed: Could not establish trust relationship
for the SSL/TLS secure channel.

How can I fix this (if i logon manually it works)

thanks

Nick

Imports System

Imports System.Net

Imports System.IO

Imports System.Xml

Imports System.Text.RegularExpressions



Module Module1

' Code to call the Authentication:

Private CookieJar As CookieContainer

Private strCookies As String

Private WebReq As HttpWebRequest

Sub Main()

Try


Dim strServerName As String = "myserver"

Dim strDomain As String = "mydomain"

Dim strUserID As String = "myuser"

Dim strPassword As String = "mypw"

Dim strUserName As String = "myuser"

Dim term As String = ""

' Create our destination URL.

Dim strURL As String = "https://" & strServerName '& "/exchange/" &
strUserName & "/inbox"

' Enter your WebDAV-related code here.

Dim QUERY As String = "<?xml version=""1.0""?>" _

& "<g:searchrequest xmlns:g=""DAV:"">" _

& "<g:sql>SELECT ""urn:schemas:httpmail:subject"", " _

& """urn:schemas:httpmail:from"", ""DAV:displayname"", " _

& """urn:schemas:httpmail:textdescription"" " _

& "FROM SCOPE('deep traversal of """ & strURL & """') " _

& "WHERE ""DAV:ishidden"" = False AND ""DAV:isfolder"" = False " _

& "AND ""urn:schemas:httpmail:subject"" LIKE '%" & term & "%' " _

& "ORDER BY ""urn:schemas:httpmail:date"" DESC" _

& "</g:sql></g:searchrequest>"

Dim Response As System.Net.HttpWebResponse

Dim RequestStream As System.IO.Stream

Dim ResponseStream As System.IO.Stream

Dim ResponseXmlDoc As System.Xml.XmlDocument

Dim SubjectNodeList As System.Xml.XmlNodeList

Dim SenderNodeList As System.Xml.XmlNodeList

Dim BodyNodeList As System.Xml.XmlNodeList

Dim URLNodeList As System.Xml.XmlNodeList

Dim Request As HttpWebRequest = CType(System.Net.WebRequest.Create(strURL),
_

System.Net.HttpWebRequest)

Request.CookieContainer = New CookieContainer()

Request.CookieContainer.Add(AuthenticateSecureOWA(strServerName, strDomain,
strUserID, strPassword))

Request.Credentials = New System.Net.NetworkCredential( _

strUserName, strPassword, strDomain)

Request.Method = "SEARCH"

Request.ContentType = "text/xml"

Request.KeepAlive = True

Request.AllowAutoRedirect = False

Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(QUERY)

Request.Timeout = 30000

Request.ContentLength = bytes.Length

RequestStream = Request.GetRequestStream()

RequestStream.Write(bytes, 0, bytes.Length)

RequestStream.Close()

Request.Headers.Add("Translate", "F")

Response = Request.GetResponse()

ResponseStream = Response.GetResponseStream()

' Create the XmlDocument object from the XML response stream.

ResponseXmlDoc = New System.Xml.XmlDocument()

ResponseXmlDoc.Load(ResponseStream)

SubjectNodeList = ResponseXmlDoc.GetElementsByTagName("d:subject")

SenderNodeList = ResponseXmlDoc.GetElementsByTagName("d:from")

URLNodeList = ResponseXmlDoc.GetElementsByTagName("a:href")

BodyNodeList = ResponseXmlDoc.GetElementsByTagName("d:textdescription")

Catch ex As Exception

End Try

End Sub

Private Function AuthenticateSecureOWA(ByVal strServerName As String, ByVal
strDomain As String, ByVal strUserName As String, ByVal strPassword As
String) As CookieCollection

Try


Dim AuthURL As System.Uri

Try

' Construct our destination URI.

AuthURL = New System.Uri("https://" + strServerName +
"/exchweb/bin/auth/owaauth.dll")

Catch ex As Exception

' Throw NewApplicationException("Error occurred while you are creating the
URI for OWA authentication!" + vbCrLf + vbCrLf + ex.Message)

End Try

'Dim WebReq As HttpWebRequest

CookieJar = New CookieContainer

' Create our request object for the constructed URI.

WebReq = CType(WebRequest.Create(AuthURL), HttpWebRequest)

WebReq.CookieContainer = CookieJar

' Create our post data string that is required by OWA (owaauth.dll).

Dim strPostFields As String = "destination=https%3A%2F%2F" & strServerName &
"%2Fexchange%2F" + strUserName + "%2F&username=" + strDomain + "%5C" +
strUserName + "&password=" + strPassword +
"&SubmitCreds=Log+On&forcedownlevel=0&trusted=0"

WebReq.KeepAlive = True

WebReq.AllowAutoRedirect = False

WebReq.Method = "POST"

' Store the post data into a byte array.

Dim PostData() As Byte = System.Text.Encoding.ASCII.GetBytes(strPostFields)

' Set the content length.

WebReq.ContentLength = PostData.Length

Dim tmpStream As Stream

Try

' Create a request stream. Write the post data to the stream.

tmpStream = WebReq.GetRequestStream()

tmpStream.Write(PostData, 0, PostData.Length)

tmpStream.Close()

Catch ex As Exception

'Throw NewApplicationException("Error occurred while trying OWA
authentication!" + vbCrLf + vbCrLf + ex.Message)

End Try



' Get the response from the request.

Dim WebResp As HttpWebResponse = WebReq.GetResponse()

WebResp.Close()

Return WebResp.Cookies

Catch ex As Exception

End Try

End Function



End Module
date: Mon, 26 Feb 2007 08:52:10 +1100   author:   Nick C

Re: Could not establish trust relationship for the SSL/TLS secure channel.   
Hello,

the certificate of the server is invalid or not trusted.

You can either add the root certificate of the server to the trusted root 
certificate store, or use this code to override the check of the .NET 
Framework:

..NET 2.0:

ServicePointManager.ServerCertificateValidationCallback = delegate { return 
true; };

..NET 1.1:

Implement the ICertificatePolicy interface (See MSDN for an example)

Best regards,
Henning Krause

"Nick C"  wrote in message 
news:Og3lQcSWHHA.5068@TK2MSFTNGP03.phx.gbl...
> Hi
>
> When i run the code below I get the follwing error:
>
> The underlying connection was closed: Could not establish trust 
> relationship
> for the SSL/TLS secure channel.
>
> How can I fix this (if i logon manually it works)
>
> thanks
>
> Nick
>
> Imports System
>
> Imports System.Net
>
> Imports System.IO
>
> Imports System.Xml
>
> Imports System.Text.RegularExpressions
>
>
>
> Module Module1
>
> ' Code to call the Authentication:
>
> Private CookieJar As CookieContainer
>
> Private strCookies As String
>
> Private WebReq As HttpWebRequest
>
> Sub Main()
>
> Try
>
>
> Dim strServerName As String = "myserver"
>
> Dim strDomain As String = "mydomain"
>
> Dim strUserID As String = "myuser"
>
> Dim strPassword As String = "mypw"
>
> Dim strUserName As String = "myuser"
>
> Dim term As String = ""
>
> ' Create our destination URL.
>
> Dim strURL As String = "https://" & strServerName '& "/exchange/" &
> strUserName & "/inbox"
>
> ' Enter your WebDAV-related code here.
>
> Dim QUERY As String = "<?xml version=""1.0""?>" _
>
> & "<g:searchrequest xmlns:g=""DAV:"">" _
>
> & "<g:sql>SELECT ""urn:schemas:httpmail:subject"", " _
>
> & """urn:schemas:httpmail:from"", ""DAV:displayname"", " _
>
> & """urn:schemas:httpmail:textdescription"" " _
>
> & "FROM SCOPE('deep traversal of """ & strURL & """') " _
>
> & "WHERE ""DAV:ishidden"" = False AND ""DAV:isfolder"" = False " _
>
> & "AND ""urn:schemas:httpmail:subject"" LIKE '%" & term & "%' " _
>
> & "ORDER BY ""urn:schemas:httpmail:date"" DESC" _
>
> & "</g:sql></g:searchrequest>"
>
> Dim Response As System.Net.HttpWebResponse
>
> Dim RequestStream As System.IO.Stream
>
> Dim ResponseStream As System.IO.Stream
>
> Dim ResponseXmlDoc As System.Xml.XmlDocument
>
> Dim SubjectNodeList As System.Xml.XmlNodeList
>
> Dim SenderNodeList As System.Xml.XmlNodeList
>
> Dim BodyNodeList As System.Xml.XmlNodeList
>
> Dim URLNodeList As System.Xml.XmlNodeList
>
> Dim Request As HttpWebRequest = 
> CType(System.Net.WebRequest.Create(strURL),
> _
>
> System.Net.HttpWebRequest)
>
> Request.CookieContainer = New CookieContainer()
>
> Request.CookieContainer.Add(AuthenticateSecureOWA(strServerName, 
> strDomain,
> strUserID, strPassword))
>
> Request.Credentials = New System.Net.NetworkCredential( _
>
> strUserName, strPassword, strDomain)
>
> Request.Method = "SEARCH"
>
> Request.ContentType = "text/xml"
>
> Request.KeepAlive = True
>
> Request.AllowAutoRedirect = False
>
> Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(QUERY)
>
> Request.Timeout = 30000
>
> Request.ContentLength = bytes.Length
>
> RequestStream = Request.GetRequestStream()
>
> RequestStream.Write(bytes, 0, bytes.Length)
>
> RequestStream.Close()
>
> Request.Headers.Add("Translate", "F")
>
> Response = Request.GetResponse()
>
> ResponseStream = Response.GetResponseStream()
>
> ' Create the XmlDocument object from the XML response stream.
>
> ResponseXmlDoc = New System.Xml.XmlDocument()
>
> ResponseXmlDoc.Load(ResponseStream)
>
> SubjectNodeList = ResponseXmlDoc.GetElementsByTagName("d:subject")
>
> SenderNodeList = ResponseXmlDoc.GetElementsByTagName("d:from")
>
> URLNodeList = ResponseXmlDoc.GetElementsByTagName("a:href")
>
> BodyNodeList = ResponseXmlDoc.GetElementsByTagName("d:textdescription")
>
> Catch ex As Exception
>
> End Try
>
> End Sub
>
> Private Function AuthenticateSecureOWA(ByVal strServerName As String, 
> ByVal
> strDomain As String, ByVal strUserName As String, ByVal strPassword As
> String) As CookieCollection
>
> Try
>
>
> Dim AuthURL As System.Uri
>
> Try
>
> ' Construct our destination URI.
>
> AuthURL = New System.Uri("https://" + strServerName +
> "/exchweb/bin/auth/owaauth.dll")
>
> Catch ex As Exception
>
> ' Throw NewApplicationException("Error occurred while you are creating the
> URI for OWA authentication!" + vbCrLf + vbCrLf + ex.Message)
>
> End Try
>
> 'Dim WebReq As HttpWebRequest
>
> CookieJar = New CookieContainer
>
> ' Create our request object for the constructed URI.
>
> WebReq = CType(WebRequest.Create(AuthURL), HttpWebRequest)
>
> WebReq.CookieContainer = CookieJar
>
> ' Create our post data string that is required by OWA (owaauth.dll).
>
> Dim strPostFields As String = "destination=https%3A%2F%2F" & strServerName 
> &
> "%2Fexchange%2F" + strUserName + "%2F&username=" + strDomain + "%5C" +
> strUserName + "&password=" + strPassword +
> "&SubmitCreds=Log+On&forcedownlevel=0&trusted=0"
>
> WebReq.KeepAlive = True
>
> WebReq.AllowAutoRedirect = False
>
> WebReq.Method = "POST"
>
> ' Store the post data into a byte array.
>
> Dim PostData() As Byte = 
> System.Text.Encoding.ASCII.GetBytes(strPostFields)
>
> ' Set the content length.
>
> WebReq.ContentLength = PostData.Length
>
> Dim tmpStream As Stream
>
> Try
>
> ' Create a request stream. Write the post data to the stream.
>
> tmpStream = WebReq.GetRequestStream()
>
> tmpStream.Write(PostData, 0, PostData.Length)
>
> tmpStream.Close()
>
> Catch ex As Exception
>
> 'Throw NewApplicationException("Error occurred while trying OWA
> authentication!" + vbCrLf + vbCrLf + ex.Message)
>
> End Try
>
>
>
> ' Get the response from the request.
>
> Dim WebResp As HttpWebResponse = WebReq.GetResponse()
>
> WebResp.Close()
>
> Return WebResp.Cookies
>
> Catch ex As Exception
>
> End Try
>
> End Function
>
>
>
> End Module
>
>
>
date: Mon, 26 Feb 2007 00:01:57 +0100   author:   Henning Krause [MVP - Exchange]

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us