HELP need: SL2 Beta1: How to authenticate myself for HttpWebRequest?
Last post 03-14-2008 7:06 PM by jjy2. 5 replies.
Sort Posts:
03-12-2008 9:02 PM
HELP need: SL2 Beta1: How to authenticate myself for HttpWebRequest?

I need help on this.

 I am trying to use HttpWebRequest to upload huge files to server by splitting the file into multiple chunks. However, seems in SL2 HttpWebRequest has some sort of CA authentication mechnism. There is no such thing like System.Net.HttpWebRequest.Credentials  ....  When I try to send data by something like

Request.BeginGetResponse

It always give security error msg. 

 Urgent. I am wondering who could help me out.

kevin_xyz2002

Joined on 02-26-2008
Posts 10
03-13-2008 12:15 AM
Re: HELP need: SL2 Beta1: How to authenticate myself for HttpWebRequest?


Is the secruity error message you mentioned is from your web app?

In my case, I make sure user is authenticated before they go to the page which hosts silverlight  control.

Then every HTTP request made by SL control will implicitly carry either cookies or credential usual way.
So your aspx app can validate user just the same way as you would normally do.


 

jjy2

Joined on 09-17-2007
Posts 145
03-13-2008 5:09 PM
Re: HELP need: SL2 Beta1: How to authenticate myself for HttpWebRequest?

The following simple piece of code will get me the 'security error' message.

 

'Try

Dim Request As HttpWebRequest = CType(WebRequest.Create(New Uri("http://localhost/a.html")), HttpWebRequest)

Request.Method = "POST"

Request.ContentType = "application/x-www-form-urlencoded"

Dim formBody As String = "fileName=" & "mytrialuploadfile.txt" & "&" & _

"fileContents=" & "8a8b8c8d8e8f"

 

Dim formBytes As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(formBody)

Request.BeginGetRequestStream(New AsyncCallback(AddressOf ContinueGettingResponse), Request)

Catch ex As Exception

txtReponse.Text = ex.Message

End Try

kevin_xyz2002

Joined on 02-26-2008
Posts 10
03-13-2008 9:02 PM
Re: HELP need: SL2 Beta1: How to authenticate myself for HttpWebRequest?

Can you post Security error details?  

For me it all work ok.

--------- in aspx page, just to check what is posted -------

  protected override void OnInit(EventArgs e)
        {
            if(this.Request.HttpMethod == "POST")
            {
                foreach (string formValue in Request.Form)
                {
                    System.Diagnostics.Debug.Write(formValue);
                }
            }
            base.OnInit(e);
            
        }

---------------------------- and your SL code -------------------

void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                WebRequest request = WebRequest.Create(HtmlPage.Document.DocumentUri);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                IAsyncResult ar= request.BeginGetRequestStream(this.Send, request);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
        }
        private void Send(IAsyncResult ar)
        {
            try
            {
                WebRequest request = ar.AsyncState as WebRequest;
                string formBody = "fileName=mytrialuploadfile.txt&fileContents=8a8b8c8d8e8f";
                Stream stream = request.EndGetRequestStream(ar);
                byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(formBody);
                stream.Write(bytes, 0, bytes.Length); 
                stream.Close();
                request.BeginGetResponse(Receive, request);
               
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
        }
        private void Receive(IAsyncResult ar)
        {
            try
            {
                WebRequest request = ar.AsyncState as WebRequest;

                WebResponse response = request.EndGetResponse(ar);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
        }

jjy2

Joined on 09-17-2007
Posts 145
03-14-2008 5:20 PM
Re: HELP need: SL2 Beta1: How to authenticate myself for HttpWebRequest?

Thank you JJY.

Though still got no luck. Security error was gone. But new troubles.

Later I decided to try your code. This is the result.

(1) If I use your code completely, which is

Dim request As WebRequest = WebRequest.Create(HtmlPage.Document.DocumentUri)

request.Method = "POST"

request.ContentType = "application/x-www-form-urlencoded"

Dim ar As IAsyncResult = request.BeginGetRequestStream(AddressOf SendData, request)

I will error out  at the first line (create the webrequest) with

[net_unknown_prefix] Arguments: Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=2.0.30226.2&File=System.Net.dll&Key=net_unknown_prefix.

 

(2) If I replace the URI with a valid page URI,  for example

Dim ServerPageUri As Uri = New Uri("http://localhost/Default.aspx")

Dim request As WebRequest = WebRequest.Create(ServerPageUri)

 

I got error at private void Send() function, at line

request.BeginGetRequestStream(AddressOf ReceiveData, request)

with the message: Operation is not valid due to the current state of the object.

 

I am getting a bit frustrating now. I wonder how come the code works for you but not for me. and it is kind of difficult to pinpoint the error and at the same time, little reference is available.

 

Thanks a lot,

kevin_xyz2002

Joined on 02-26-2008
Posts 10
03-14-2008 7:06 PM
Re: HELP need: SL2 Beta1: How to authenticate myself for HttpWebRequest?


I think you should post your sample project file to somewhere so people can have a look.

I will also have a look to see if I can help

jjy2

Joined on 09-17-2007
Posts 145