Quantcast
Viewing all articles
Browse latest Browse all 1083

Cannot set CookieContainer due to the state of the HttpWebRequest object.

Hi

I have Silverlight OOB application as signalR Client and Web application as signalR Hub Server.

Previously I used WCF RIA for login users of SL application to server and SignalR Hub on server recognized each user identity successfuly.

Now I want to abandon WCF RIA and use signalR for client authentication.

Following is my code:

Public Property UserName As String
    Public Property Password As String
    Dim address As New Uri("http://localhost:39170/RemoteLogin.aspx")


Start Request:

Public Sub StartRequest()

        WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp)
        System.Net.WebRequest.RegisterPrefix("http://", WebRequestCreator.BrowserHttp)
        System.Net.WebRequest.RegisterPrefix("https://", WebRequestCreator.BrowserHttp)
        Dim request As HttpWebRequest = WebRequest.Create(address)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        UserName = "myusername"
        Password = "mypassword"
        request.CookieContainer = New CookieContainer()
        ' Prepare the request asynchronously.
        request.BeginGetRequestStream(AddressOf CreateRequest, request)
 End Sub


Create Request:

Private Sub CreateRequest(ByVal asyncResult As IAsyncResult)
        Dim request As HttpWebRequest = CType(asyncResult.AsyncState, HttpWebRequest)
        Dim requestStream As Stream = request.EndGetRequestStream(asyncResult)
        Dim writer As New StreamWriter(requestStream)
        writer.Write("UserName=" & UserName + "&Password=" & Password)
        writer.Close()
        requestStream.Close()
        ' Read the response asynchronously.
        request.BeginGetResponse(AddressOf ReadResponse, request)
End Sub


Read Response:

Private Sub ReadResponse(ByVal asyncResult As IAsyncResult)
        Dim result As String
        Dim request As HttpWebRequest = CType(asyncResult.AsyncState, HttpWebRequest)
        ' Get the response stream.
        Dim response As HttpWebResponse = request.EndGetResponse(asyncResult)
Dim responseStream As Stream = response.GetResponseStream()
        Try
            ' Read the returned text.
            Dim reader As New StreamReader(responseStream)
            Dim MyStream As String = reader.ReadToEnd()
            result = MyStream & "This is the stream "
            request.CookieContainer = New CookieContainer()
            Dim authCookie As Cookie = response.Cookies(".ASPXAUTH")
            Dim Connection = New HubConnection("http://localhost:39170 ")
            Connection.CookieContainer.Add(address, authCookie)
            Dim updateAction As New Action(Of String)(AddressOf UpdateText)
            ' Call the action and pass in the new text.
            Dispatcher.BeginInvoke(updateAction, result)
        Catch
            result = "Error contacting service."
        Finally
            response.Close()
        End Try
    End Sub

    Private Sub UpdateText(text As String)
        TBl.Text = text
    End Sub


And the Sub on RemoteLogin.aspx page is:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim username As String = Request("UserName").Trim
        Dim password As String = Request("Password").Trim
        Dim result = Membership.ValidateUser(username, password)
        If result Then
            FormsAuthentication.SetAuthCookie(username, False)
        End If
End Sub


But I am getting the error in Sub() ReadResponse on line:

            request.CookieContainer =NewCookieContainer()

The error is:

“Cannot set CookieContainer due to the state of the HttpWebRequest object.”

Can some one please guide me what I am doing wrong?

Thanks


Viewing all articles
Browse latest Browse all 1083

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>