Friday, March 29, 2013

FIX: System.DirectoryServices.DirectoryServicescomException - Operation Error

At times you might caught up in an issue that your ASP.NET website is working fine in one environment but authentication is failing on other environments when you have used Identity providers.

To fix this issue -
Make sure, you have "Basic Authentication", "Windows Authentication", and "ASP.NET Impersonate" enabled for the website in IIS.

These settings needs to be enabled in such cases.

Saturday, February 2, 2013

FIX: An MVC website configured for Windows Authentication is trying to redirect to Login page while accessing from IIS

I came across a very strange issue recently. My MVC application was configured to use Windows Authentication, and it was deployed on IIS.
While the website was working very properly when accessed from Development environment, means, it was taking the routes into considerations and was properly redirecting the user to specific routes. However, while accessing the website from IIS, it was straight-away redirecting user to a login page. (which obviously was throwing the error, as the windows application is not suppose to have login page).
I checked all configurations on IIS, and also could see only 'Windows Authentication" enabled, which was quite correct.
Though I haven't found any particular reason why it would be happening, however, I learned how to fix this :)
Add following keys in "<appSettings>" section of application's web.config file.


<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>


and restart the site on IIS. That should do the magic!
If the issue is too strange, the solution is more than strange...

Hope this saves someone's time and effort :)

Thursday, December 20, 2012

@Html.ActionLink to call action in different controller

@Html.ActionLink has many overloads (at least 17), and so sometimes it becomes confusing if the operation is not so straight-foward. 

For example, if you want to execute an Action of different controller, you must take care to use it like following:

@Html.ActionLink("Link Text", "MyAction", "MyController", new {id = item.ID}, null)
 

Its important to pass the last parameter (which is, "hostname"),
eventhough if you pass it as "null". Doing this will take up proper overload and will call the action from different controller.


Friday, December 7, 2012

Investigations:Could not load file or assembly or one of its dependencies.An attempt was made to load a program with an incorrect format.

ERROR: Could not load file or assembly or one of its dependencies.An attempt was made to load a program with an incorrect format. 

Developers generally face this issue in production server, or running a web application under IIS.
The site works fine in the integrated development environment, but fails and giving this error while running it in IIS.
The most likely reason of this issue is the incompatibility of target platforms. 
Say, your  application is targettng to only x32, but the dependent assembly targets to "Any CPU", or vice versa.

In order to fix this issue, you need to change settings of relevant Application Pool in IIS, to allow 32-bit applications.



This should resolve the issue.

Tuesday, November 27, 2012

Investigations: Could not load type 'System.Web.Razor.Parser.[TypeName]' from assembly 'System.Web.Razor, Version=2.0.0.0'

I recently faced this issue while working on my ASP.NET MVC 4 project over .NET Framework 4.5.

System was throwing following error whenever I make a call to any member of RazorEngine.Razor class.
Like Razor.Parse, etc.
In order to investigate the issue, I checked at various places in my project and GAC, and finally I could see following been placed in web.config file of  my project. It seems it is incorrectly creating the binding redirect for System.Web.Razor.


      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>

Remove this section from web.config file, and the issue should be resolved now.

Wednesday, November 21, 2012

Investigations: How to identify current path in referenced library in MVC application?


Following is the exact scenario in my application:

1) MVC application is referencing one DLL (a Class library project).
2) some code in the class library need to access folders and files from the MVC application.
Say, there is one folder named as "Templates" in root of my MVC application.
How should I able to get the path of that folder in context of execution path from any method of the class library.
If I try "~\templates" or "\templates", it looks for folder in C:\Program Files\... instead of the websites folder.
If I try "templates", it looks for folder in C:\ only.
any idea of how to achieve this


Tuesday, November 13, 2012

Microsoft.Commom.targets Warning# 983 The reference assemblies for Framework ".NET Framework, Version=X.x" were not found

Following is the exact error message:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets (983): The reference assemblies for framework ".NETFramework,Version=v4.5" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

This occurs when your Build server does not have Visual Studio 2012 (which ideally should not be anyway), and it has only .NET Framework 4.5, and no Framework 4.5 SDK or .NET Framework Framework Targetting-Pack is not installed.

In order to get rid of these warning messages, you have two options:
1) Install SDK or Targetting-Pack for .NET Framework 4.5. But they are considerably large in size.
2) The other option is to copy following folder from your development machine to your TFS build machine.

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5

Doing this should fix the issue, and the warning messages will be disappeared.