4

I found the property SuppressFormsAuthenticationRedirect on this post, but when I tried to use it:

Response.StatusCode = (int)HttpStatusCode.Unauthorized;
Response.SuppressFormsAuthenticationRedirect = true;

I get a build error:

Error   53  'System.Web.HttpResponseBase' does not contain a definition for 'SuppressFormsAuthenticationRedirect' and no extension method 'SuppressFormsAuthenticationRedirect' accepting a first argument of type 'System.Web.HttpResponseBase' could be found (are you missing a using directive or an assembly reference?)   Controllers\ErrorController.cs  39  26  Roving

So I threw in a breakpoint, inspected Response in the watch window, and found that it does indeed have the property. So I tried setting it in the immediate with Response.SuppressFormsAuthenticationRedirect = true which did not cause an error, and it worked as expected. So why is this a build error? I did this, just for fun, and found that it worked as expected (but this is pretty hacky):

Response.StatusCode = (int)HttpStatusCode.Unauthorized;
((dynamic)Response).SuppressFormsAuthenticationRedirect = true;
Community
  • 1
  • 1
xdumaine
  • 10,096
  • 6
  • 62
  • 103
  • Is your project set to build against .Net 4, as opposed to .Net 4.5? – Amith George Jul 27 '13 at 21:05
  • 1
    Second, how are you able to reach the Debug part, if your app won't compile/build in the first place? – Amith George Jul 27 '13 at 21:06
  • @AmithGeorge Yes, I did did see on [this page](http://msdn.microsoft.com/en-us/library/system.web.httpresponse.suppressformsauthenticationredirect.aspx) that it appears to only be supported in 4.5, but then why would the property be there, and accessible at runtime? – xdumaine Jul 27 '13 at 21:07
  • @AmithGeorge Debugging/inspecting it without that line (commented out). – xdumaine Jul 27 '13 at 21:07
  • That's cuz you must be having .Net 4.5 installed on your local machine. VS thinks that the eventual deployment environment will have only .Net 4.0, hence it prevents the build. – Amith George Jul 27 '13 at 21:08
  • .Net 4.5 is an in-place update over .Net 4.0. If 4.5 is installed, then `Response` has that property. – Amith George Jul 27 '13 at 21:11
  • I am getting this error. Both my website and it's component DLL assembly are targeted at 4.6.1. I have dropped and recreated my reference to System.Web, and I still get the error. The hack above at least allows compilation, but anyone got any better answer? – Steve Hibbert Dec 16 '15 at 16:34

1 Answers1

0

Amith George suggested in the comments that this was because I had .NET 4.5 installed on my machine, but was targetting .NET 4.0. Because .NET 4.5 is an in-place upgrade over 4.0, that DLL was being used, and thus the variable had the SuppressFormsAuthenticationRedirect property at runtime. The build was failing properly because when compiling against .NET 4, it is not aware of that property.

xdumaine
  • 10,096
  • 6
  • 62
  • 103