forms authentication - email current page as html

Last post 05-20-2008 5:41 AM by DoaX. 5 replies.

Sort Posts:

  • forms authentication - email current page as html

    02-09-2007, 12:05 AM
    • Member
      point Member
    • briancollins
    • Member since 02-07-2007, 12:29 AM
    • Posts 4

    I am trying to send the contents of the page my users are currently logged into (i.e. "timesheet.aspx") as an email with the page shown in the body as html.  My users access the page using Forms Authentication via an LDAP/Active Directory connection string and login control.  My web.config seems to be configured correctly.  When the users submit the page via a button click event, the email that is sent contains the html from the login screen (login.aspx). 

    I'm assuming that somehow the user authentication isn't persistent when attempting to send the contents of the current page as an html email and the site is redirecting to the login screen.  Any suggestions?

    Please advise... 

  • Re: forms authentication - email current page as html

    02-09-2007, 6:09 PM
    • Member
      177 point Member
    • webdevl
    • Member since 07-06-2003, 8:11 PM
    • Leesburg, VA
    • Posts 38
    What is happening on the button click (aka how are you generating the page into html to send)?  Post a code snippet if possible
  • Re: forms authentication - email current page as html

    02-09-2007, 7:59 PM
    • Member
      point Member
    • briancollins
    • Member since 02-07-2007, 12:29 AM
    • Posts 4

    Here's the function to obtain the page and then send it as an html email:

     Private Function ScreenScrapeHtml(ByVal url As String) As String
            Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
            Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
            Dim result As String = sr.ReadToEnd()
            sr.Close()
            Return result
        End Function 'ScreenScrapeHtml
    -------------------------------------------------------------------------------------------------------------------------------------------
        Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            Dim message As _
                New System.Net.Mail.MailMessage("from@email.com", "to@email.com")
            message.Subject = "ECI Time Sheet - " + tbEmployee.Text
            'screen scrape the html
            Dim html As String = ScreenScrapeHtml("https://url.of.pageiwanttosend.aspx")
            message.Body = html
            message.IsBodyHtml = True
            Dim smtp As New System.Net.Mail.SmtpClient("192.168.x.x")
            smtp.Send(message)
        End Sub
    End Class

  • Re: forms authentication - email current page as html

    02-10-2007, 12:34 PM
    • Member
      177 point Member
    • webdevl
    • Member since 07-06-2003, 8:11 PM
    • Leesburg, VA
    • Posts 38

    you are correct about the credetials being dropped.  The reason is that the server is making the request and not the client and the client is what has the credentials.  A couple suggestions are

    1.  Add the credentials to the request by adding

    objRequest.Credentials = CredentialCache.DefaultCredentials

    which will pass the current credentials of the client to the request.

    or

    us this method to capture the output of the page http://west-wind.com/weblog/posts/481.aspx by overriding the render of the page.

     

    Please repost if you need more information.  If this solves your problem please mark as an answer to close the post.

  • Re: forms authentication - email current page as html

    02-11-2007, 1:35 PM
    • Member
      point Member
    • briancollins
    • Member since 02-07-2007, 12:29 AM
    • Posts 4

    i added the credentials directly under my declaration of objRequest as such:

     Private Function ScreenScrapeHtml(ByVal url As String) As String
            Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
            objRequest.Credentials = CredentialCache.DefaultCredentials
            'tbEmployee.Text = objRequest.Credentials
            Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
            Dim result As String = sr.ReadToEnd()
            'sr.Close()
            Return result
        End Function 'ScreenScrapeHtml

    this still returns the login screen instead of the active page content.  ideas?  I tried using your output referred to in the west-wind.com post, but wasn't successful there either, probably in the conversion from C# to VB.

  • Re: forms authentication - email current page as html

    05-20-2008, 5:41 AM
    • Member
      3 point Member
    • DoaX
    • Member since 04-11-2007, 6:14 AM
    • Posts 40

    I have the same problem ...

    This doesn't work:

     

    WebRequest wreq = System.Net.HttpWebRequest.Create(Request.Url.OriginalString);
    wreq.Credentials = CredentialCache.DefaultCredentials;
    WebResponse wrsp = wreq.GetResponse();
    
    StreamReader sr = new StreamReader(wrsp.GetResponseStream());
    string strHTML = sr.ReadToEnd();
    sr.Close();
      
Page 1 of 1 (6 items)