ELMAH: Can you set it up to email errors only remotely? - asp.net

While debugging my solution locally, I would like to not receive ELMAH error emails. I'd only like to receive them when the application is hosted on a remote machine. Is this possible?

I faced the same problem and rejected the idea of having different web.configs for local and remote as that just doesn't play nicely with source control (but see comments below).
Instead, I added this to a Global.asax file in each site which uses Elmah:
void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
e.OnErrorMailing(); // Prevent local machine errors from being mailed to the rest of the team
}
OnErrorMailing() is an extension method declared in our web library as follows:
/// <summary>
/// Prevent local machine errors from being mailed to the rest of the team. Feel free to add your own machine names/case blocks here
/// </summary>
public static void OnErrorMailing(this ErrorMailEventArgs e)
{
switch (e.Error.HostName.ToLower())
{
case "mymachinename":
e.Mail.To.Clear();
e.Mail.To.Add("MYEMAILADDRESS");
break;
}
}
Of course if you have only one site which uses Elmah or you don't mind code duplication you can put that code straight into global.asax too.
I've no doubt there are more elegant solutions but this works for me. Only I receive the exceptions from my local machine and production exceptions go to the email address configured in web.config.

I'm pretty sure you can do this. Just filter outgoing error emails. Of course use some setting to indicate that it's hosted remotely.
Or just have two different web.configs and make sure "dev one" has emaling turned off.

You can have emails go to a pickup folder on your local machine instead of the SMTP server by modifying the web.config file
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\temp\" />
</smtp>
</mailSettings>
</system.net>
Or you can put this in your Machine.config settings on your dev box so you don't have to keep touching the web.config file.

I found a solution which is to use config transforms with VS 2010. I removed the <errorMail /> tag from my base web.config then added it to my Web.Qa.Config and Web.Release.Config with transform tags. When I publish, it allows the remote machines to send out the emails while they are suppressed locally.

Related

Encrypting connection strings causes App Pool to stop

I want to encrypt my connection strings when I first run my code. I call the code below from the Global.asax.
When I try this on my machine, the connectionstrings are encrypted, but when I place this on the server it causes the app pool to stop. I can't really tell why this happens because the connection is reset.
Is this a problem with the write settings of a file? How can I make this work on the web server?
var config = WebConfigurationManager.OpenWebConfiguration("~");
var section = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (section == null)
{
return;
}
if (section.SectionInformation.IsProtected)
{
return;
}
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();
I know this is an old question but I just now ran across it.
If you encrypted the connection strings in the web.config on your local machine before copying the web.config file over to your web server, then I'm assuming that's the problem. Unless both machines are using the same IIS RSA key (which I'm not even sure is possible and it's probably unlikely), the destination machine won't be able to decrypt the connection string cipher because it was encoded with the source machine's key.
Therefore, you must encrypt the web.config settings from the target machine itself.
By the way, I got confused by your comments under the question. Invoking the config.Save() method will modify the web.config file, even if the content didn't change. In the example code above, the content should have changed anyway because you encrypted the connectionStrings section before saving. In order for IIS to utilize an updated web.config, it has to recycle its application pool first. This is partly why, by default, IIS automatically recycles the associated app pool when it detects a web.config change.

ASP.net Identity can't login

I'm using Asp.net Identity and I've deployed my web role to Azure, and changed my connection string in Web.config so it looks like this:
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=SERVERNAME,1433;Database=DATABASE;User ID=USER;Password=PASSWORD;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
I haven't changed default Account controller, but when I try to Login nothing nothing happens except that URL changes to "/Account/Login?ReturnUrl=%2FRoutines" which should happen if user successfully logged in (no errors are shown)
Why is this happening ? (and how can I fix it)
EDIT
Here is code which configures ASP.net Identity
public class DatabaseContext : IdentityDbContext<User>
{
public DatabaseContext()
: base("DefaultConnection")
{
Configuration.LazyLoadingEnabled = true;
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<User>()
.ToTable("Users");
var usermapping = new UserMapping();
usermapping.MapUser(modelBuilder);
}
I've noticed that now even when I'm using LocalDb I can't Log in, and I don't know why is this because, I haven't changed my code, only changes that I made to the project is Added Azure web service, and Asp.net web Api 2 project (and when I've tested locally I didn't run my web api project), before this everything worked fine.
Are you sure your connection is really used when you deploy (in Web.config.release)?
In case you are, try testing the website locally (with the Azure SQL connection string) and stepping through the code for POST version of Login. There you should be able to see, what exactly is going on. You will probably need to enable access to SQL from your IP address, which is easy to do - just go to Azure Portal, click on your SQL database and down below select Manage, which will automatically ask you for your IP address access permission.
I don't know what I exactly happened, but I've fetched older version of solution from TFS and that helped (eve though code was same)

Error when call google map static api in asp.net web application:The remote name could not be resolved:'maps.googleapis.com'

I used google map static api to get map image, but I got an error "The remote name could not be resolved:'maps.googleapis.com'" when executing using (HttpWebResponse response = request.GetResponse() as HttpWebResponse).
But I can got image of google map in windows application(same code with web application), I don't know what happened
This thing is happen due to proxy setting in your system.Please add this code in your web.config file and it will work.
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
</defaultProxy>
</system.net>
The problem can go from invalid url (hostname) to your firewall or proxy blocking the request or even misconfigured host file or DNS.
I also faced same issue.
If you are connected with local area network and protected by Proxy server then Go in google chrome settings.
Our system takes default settings from google chrome.
In settings of chrome, go in proxy settings.Click on Lan setting.Click on proxy setting checkbox and click as well on by pass proxy server for local addresses.Also fill details of your proxy server in textbox.
Go in advance and in exceptions field type localhost.Run project again.It will work.

ASP PasswordRecovery Email Failure, DateTime precision

I have an asp.net 4.0 website and I'm using the PasswordRecovery control for a forgot password form. When I run the site locally it works fine, emails are sent. However, when I run the site from my vps, I get an error message when trying to send the email. There's nothing in the server's event log.
My PasswordRecovery aspx code is as follows:
<asp:PasswordRecovery ID="PasswordRecovery1" runat="server"
CssClass="mediumText">
<MailDefinition From="noreply#x.com" BodyFileName="~/EmailTemplates/PasswordRecovery.txt" />
My web.config mail settings are as follows:
<system.net>
<mailSettings>
<smtp from="noreply#x.com">
<network host="smtp.123-reg.co.uk" password="x" userName="x" />
</smtp>
</mailSettings>
</system.net>
I've now run SQL Profiler against the SQL Server Express Instance, and it turns out that an exception is being thrown from the SQL Server on the call to dbo.aspnet_Membership_GetUserByName. There's a type conversion issue because PasswordRecovery is passing a DateTime parameter with 7 decimal places for the seconds. If I manually execute the stored procedure with 3 decimal place,s then it works. Does anyone know why the precision of the DateTime parameter is different on my server than on my laptop?
As you will see from reading this issue and the subsequent responses, the DateTime precision issue you are seeing is just a result of SQL Profiler displaying the date with the wrong format. That issue is unrelated to what is preventing your email from being sent.
What is the error message you receive when you try to send the email? The issue is more than likely related to something preventing your email from making it to the SMTP server. Are you sending to the same SMTP using the same credentials from your machine?
I think it is some connectivity issue from your VPS to SMTP server.
You can test this outside of your code by trying to connect to SMTP server using telnet.
Follow the steps mentioned in the link below to
http://support.microsoft.com/kb/323350
The datetime conversion error is a red herring. That is only occuring when you copy it from the profiler and execute it in management studio. This is a known issue.
You might want to read this:
http://forums.asp.net/t/1398826.aspx/1
Sounds like it might simply be that the membership provider settings are different with regards to the passwords.
There are two possibilities that I see.
You don't have a machineKey defined in your web.config AND your local and VPS instance are both pointing to the same database. If this is true, put a machineKey in your config and regen your passwords.
Your membership provider config on the VPS used to be configured for hashed passwords and somewhere along the way was changed to encrypted passwords. (or vice versa). This would also cause issues.

SMTPClient Half Working \ Half Not

I am using Microsoft's membership framework on a website. I am able to send password retrieval e-mails, but am unable to send e-mails myself using SMTPClient.
When you configure the SMTP settings in the Web Site Administration Tool, what are the settings that this application give it's SMTPclient?
If I know this, I can duplicate it and hopefully send e-mails.
A few items I've ruled out.
- I'm using port 25, (which is the only one allowed under medium trust).
- This code works on my local system.
I was attempting to pass the values for SMTPClient in with the constructor. What I failed to realize is that SMTPClient apparently automatically pulls the values from web.config and uses those.
By trying to pass my own values (even though they where identical); I inadvertently violated trust levels causing a security exception.
Don't pass in smtp info in the constructor, use web.config to set it up, and there should be no problem in medium trust.
It could be lots of things, from credential problems, DNS issues, or...who knows. Is it unable to send as in you get an error or as in they appear to go but never arrive?
Are you sure the WSA tool is going through SMTPClient, or are you just assuming it (I don't know myself)?
-- MarkusQ
I am getting a Security Exception on
my mail
That sounds like a credentials problem then, or a trust issue. Are you sure you're running at medium (or higher) trust? What's the rest of the error?
-- MarkusQ
Try checking your web.config file: is the WSA tool updating these settings?
This element is under the configuration element
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="you#email.com">
<network
host="your.gateway.com"
userName="your#email.com"
password="your_password"
port="25" />
</smtp>
</mailSettings>
</system.net>

Resources