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?
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
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
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.
briancollins
0 Points
4 Posts
forms authentication - email current page as html
Feb 09, 2007 04:05 AM|LINK
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...
webdevl
Member
177 Points
38 Posts
Re: forms authentication - email current page as html
Feb 09, 2007 10:09 PM|LINK
briancollins
0 Points
4 Posts
Re: forms authentication - email current page as html
Feb 09, 2007 11:59 PM|LINK
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
webdevl
Member
177 Points
38 Posts
Re: forms authentication - email current page as html
Feb 10, 2007 04:34 PM|LINK
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.
briancollins
0 Points
4 Posts
Re: forms authentication - email current page as html
Feb 11, 2007 05:35 PM|LINK
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.
DoaX
Member
3 Points
40 Posts
Re: forms authentication - email current page as html
May 20, 2008 09:41 AM|LINK
I have the same problem ...
This doesn't work: