How to add servicePointManager to web config in ASP.net MVC - asp.net-mvc

I am trying to add the following lines to web config
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
But it returning error Only one element allowed. It must be the first child element of the root element
Can any one please help on us?

try this in webconfig or refer MSDN for details
<configuration>
<system.net>
<settings>
<servicePointManager
checkCertificateName="false"
checkCertificateRevocationList="false"
/>
</settings>
</system.net>
</configuration>

Related

internal server error when I add settings of Imageresizer watermark plugin in web.config asp.net mvc

When I add settings of Imageresizer watermark plugin I've got internal server error.I just add a text bottom of Image in the left I've added code at last of web.config file for example.
<configuration>
<resizer>
<watermarks>
<text name="test2" text="store" vertical="true" align="bottomleft" />
</watermarks>
</resizer>
</configuration>
I've founded I have to add resizer name in configsection that's it.
<section name="resizer" type="ImageResizer.ResizerSection" />

Get Mail Setting from Web.Config in SendAsync Method?

I am working on Forgot Password Functionality. In my web.config file I have done the mail settings:
<system.net>
<mailSettings>
<smtp from="email">
<network host="host" port="25" userName="" password="=" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
In my SendAsync method I am trying to read setting from web.config:
SmtpClient client = new SmtpClient();
return client.SendMailAsync(ConfigurationManager.AppSettings["SupportEmailAddr"],
message.Destination,
message.Subject,
message.Body);
I have no idea what is this: AppSettings["SupportEmailAddr"]
I took this from here.
It is giving me following exception:
Value cannot be null. Parameter name: from
In your web.config file you have a section called: <appSettings>.
That is what ConfigurationManager.AppSettings is referring too.
["SupportEmailAddr"] is looking at a specific setting called SupportEmailAddr.
In your web.config it would look something like this:
<appSettings>
<add key="SupportEmailAddr" value="someone#example.com" />
</appSettings>
You are getting the value cannot be null message because you will not have the setting in your web.config as above.
So to fix the error message find your <appSettings> and add:
<add key="SupportEmailAddr" value="someone#example.com" />
Alternatively, if you have the current value in your AppSettings already then just change the key that you are looking for in the C# code.
ConfigurationManager.AppSettings["CorrectAppSettingKey"]
Note: if you plan on using any of the web.config inheritance features you should WebConfiguratonManger.AppSettings instead of ConfigurationManager.AppSettings. See the difference between the two here: What's the difference between the WebConfigurationManager and the ConfigurationManager?

MVC web.config compilation debug=true - a manual change needed?

When one creates a MVC project using the the built-in templates (File -> new Project etc) the web.config reads
<compilation debug="true" targetFramework="4.5" />
Inside the Web.Release.config I see this transform
<compilation xdt:Transform="RemoveAttributes(debug)" />
so I was assuming that when I build in Release mode the debug="true" would disappear. However I am not seeing this.
Is the transform only applied when publishing the website? I've seen websites deployed by drag & drop, with the Web.config copied over from the root (with debug="true" included).
I'd just like to check, if deploying via drag & drop do you have to remove this attribute manually? Or am I missing something?
Preferred would be to publish your web-site (different to F5 or drag and drop) so to update\transform the web.config ...
Disable debug mode
Depends on build configuration rather than destination environment, the debug attribute is for the Release build specifically when you typically want debugging disabled regardless of which environment you are deploying to.
Visual Studio project templates will create a Web.Release.config transform file with code that removes the debug attribute from the compilation element.
Below the default Web.Release.config: (with some sample transformation code that is commented out, it includes code in the compilation element that removes the debug attribute:)
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>

Run WIF without LoadUserProfile = True is throwing null error

I am using WIF SSO for authentication in my website. Everything works perfect in development environment. But on deployment I got issue
Message: The data protection operation was unsuccessful. This may have
been caused by not having the user profile loaded for the current
thread's user context, which may be the case when the thread is
impersonating. ExceptionStackTrace: at
System.Security.Cryptography.ProtectedData.Protect(Byte[] userData,
Byte[] optionalEntropy, DataProtectionScope scope) at
Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Encode(Byte[]
value)
Searching abt this issue leads me to this stackoverflow question
Is it possible to run WIF without LoadUserProfile = True
I added the code mentioned but now I am getting
Value cannot be null
I am getting e.ServiceConfiguration.ServiceCertificate ServiceCertificate null. My question is what kind of certificate is this and where can I define this in my config. Do I need to place the same certificate on ACS.
here is my config section
<microsoft.identityModel>
<service>
<audienceUris>
<add value="http://localhost:9494/" />
</audienceUris>
<federatedAuthentication>
<wsFederation passiveRedirectEnabled="true" issuer="https://devworks-sb.accesscontrol.appfabriclabs.com/v2/wsfederation" realm="http://localhost:9494" requireHttps="false" />
<cookieHandler requireSsl="false" />
</federatedAuthentication>
<applicationService>
<claimTypeRequired>
<!--Following are the claims offered by STS 'https://devworks-sb.accesscontrol.appfabriclabs.com/'. Add or uncomment claims that you require by your application and then update the federation metadata of this application.-->
<claimType type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" optional="true" />
<claimType type="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" optional="true" />
<!--<claimType type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" optional="true" />-->
<!--<claimType type="http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider" optional="true" />-->
</claimTypeRequired>
</applicationService>
<issuerNameRegistry type="Microsoft.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<trustedIssuers>
<add thumbprint="BE9D0A516BEC2BC820C23D5C2EA79F068C094382" name="https://devworks-sb.accesscontrol.appfabriclabs.com/" />
</trustedIssuers>
</issuerNameRegistry>
</service> </microsoft.identityModel>
thanx
First thing you mentioned that the problem occurred after deployment, is that right? In your web.config have you changed the audienceUris to http://whatever_service_name.cloudapp.net?
<audienceUris>
<add value="http://localhost:9494/" /> <== This is wrong
</audienceUris>
Next your question about certificate is NULL at e.ServiceConfiguration.ServiceCertificate, please verify the following:
A. Endpoint is added in your application Service Definition:
B. Certificate thumbprint is set in Service Configuration
C. Certificate is set in web.config which is correct above
D. Finally added the following in your web.config so certificate can be search by thumbprint:
<serviceCertificate>
<certificateReference x509FindType="FindByThumbprint" findValue="CERT_THUMB" />
</serviceCertificate>
Study these two resources which will be very helpful:
http://www.jimandkatrin.com/CodeBlog/post/Troubleshooting-Azure-issues.aspx
http://blogs.msmvps.com/marcelmeijer/blog/2012/05/04/windows-azure-wif-access-control-acs/
The root cause is likely to be you’re using DPAPI (the default configuration of WIF). Please try to do a few modifications for the application to work in Windows Azure. I would like to suggest you to check http://msdn.microsoft.com/en-us/IdentityTrainingCourse_WIFonWAZLab2010 for a tutorial.
Best Regards,
Ming Xu.

NLog throws configuration exception on all aspnet layout renderers

I have been working to set up NLog v2 on my ASP.NET MVC 3 application and it has worked very well so far. (I'm using the package from the offical nuGet repository) However, when I try to change the log layout to include any of the aspnet-* layout renderers, I get a configuration error. I've reduced the problem to the following minimum use case:
In the configSections block:
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
The Nlog block:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
<variable name="logDirectory" value="C:\Logs" />
<targets>
<target name="logFile" xsi:type="File" fileName="${logDirectory}\app.log"
layout="${aspnet-user-identity}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
If I change layout use any combination of renderers that are not part of the aspnet* family, this works well (I haven't tested every one, but I've looked at quite a few). The error I get is here:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: An error occurred creating the configuration section handler for nlog: Exception occurred when loading configuration from C:\..[snip]..\web.config
Source Error:
Line 16: </configSections>
Line 17:
Line 18: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
Line 19: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
Line 20:
I have really no idea what's going on. I'm not sure what about that renderer causes the configuration to become invalid. I've been banging around at it most of the day and have gotten nowhere, so I'm hoping someone here can help.
Thank you!
Make sure you have referenced the NLog.Extended assembly which is where those layouts are defined and which must have been added by the NuGet package as well to the references:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true">
<extensions>
<add assembly="NLog.Extended" />
</extensions>
<variable name="logDirectory" value="C:\Logs" />
<targets>
<target name="logFile"
xsi:type="File"
fileName="${logDirectory}\app.log"
layout="${aspnet-user-identity} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
As of NLog 4.0 the ASP.NET renderes are now in Nlog.Web
http://nlog-project.org/2015/06/13/NLog-Extended_NLog-Web_and_NLog-Windows-Forms.html
Alternative solution if Darin's doesn't work
You must have NLog.Extended referenced as Darin mentions
http://nuget.org/packages/NLog.Extended
As of NLog 2.0 you do not need to add reference in the configuration XML.
My problem was that I had no hard references to NLog.Extended in my web layer (where my web.config is) so the compiler wasn't copying the file where it needed to be.
This can be easily fixed by adding a hard reference to NLog.Extended that is a no-op wherever you are configuring your logging:
//forces the compiler to include NLog.Extensions in all downstream output directories
private static AspNetApplicationValueLayoutRenderer blank = new AspNetApplicationValueLayoutRenderer();
In my case I was using extension of le_nlog and for a reason, it was not installed in the app !
so I installed *le_nlog* by doing so :
PM> Install-Package le_nlog

Resources