Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added fix for forms authentication redirect

  • Loading branch information...
commit d3c135f458fe3ac285f70ea3b42f93cbe0794692 1 parent f5b24ec
@desunit desunit authored
View
1  CustomAuthenticationMvc/CustomAuthenticationMvc.csproj
@@ -201,6 +201,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
+ <Compile Include="Helper\SuppressFormsAuthenticationRedirectModule.cs" />
<Compile Include="Models\AccountModels.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
View
1  CustomAuthenticationMvc/Global.asax.cs
@@ -7,6 +7,7 @@
using System.Web.Optimization;
using System.Web.Routing;
using CustomAuthenticationMvc.App_Start;
+using CustomAuthenticationMvc.Helper;
namespace CustomAuthenticationMvc
{
View
73 CustomAuthenticationMvc/Helper/SuppressFormsAuthenticationRedirectModule.cs
@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using CustomAuthenticationMvc.Helper;
+using Microsoft.Web.Infrastructure.DynamicModuleHelper;
+
+[assembly: PreApplicationStartMethod(typeof(SuppressFormsAuthenticationRedirectModule), "Register")]
+
+namespace CustomAuthenticationMvc.Helper
+{
+ /// <summary>
+ /// Prevents Forms Authentication Login Page Redirect for AJAX requests:
+ /// http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx
+ /// </summary>
+ public class SuppressFormsAuthenticationRedirectModule : IHttpModule
+ {
+ private static readonly object SuppressAuthenticationKey = new Object();
+
+ public static void SuppressAuthenticationRedirect(HttpContext context)
+ {
+ context.Items[SuppressAuthenticationKey] = true;
+ }
+
+ public static void SuppressAuthenticationRedirect(HttpContextBase context)
+ {
+ context.Items[SuppressAuthenticationKey] = true;
+ }
+
+ public void Init(HttpApplication context)
+ {
+ context.PostReleaseRequestState += OnPostReleaseRequestState;
+ context.EndRequest += OnEndRequest;
+ }
+
+ private void OnPostReleaseRequestState(object source, EventArgs args)
+ {
+ var context = (HttpApplication)source;
+ var response = context.Response;
+ var request = context.Request;
+
+ if (response.StatusCode == 401 && request.Headers["X-Requested-With"] ==
+ "XMLHttpRequest")
+ {
+ SuppressAuthenticationRedirect(context.Context);
+ }
+ }
+
+ private void OnEndRequest(object source, EventArgs args)
+ {
+ var context = (HttpApplication)source;
+ var response = context.Response;
+
+ if (context.Context.Items.Contains(SuppressAuthenticationKey))
+ {
+ response.TrySkipIisCustomErrors = true;
+ response.ClearContent();
+ response.StatusCode = 401;
+ response.RedirectLocation = null;
+ }
+ }
+
+ public void Dispose()
+ {
+ }
+
+ public static void Register()
+ {
+ DynamicModuleUtility.RegisterModule(
+ typeof(SuppressFormsAuthenticationRedirectModule));
+ }
+ }
+}
View
13 CustomAuthenticationMvc/Views/Home/Index.cshtml
@@ -23,7 +23,6 @@
You are authenticated with <em>@User.Identity.Name</em>. You can run HelloService which requires Authentication:<br />
@using (Html.BeginForm("RunHelloService", "Home")) {
<input type="submit" value="Invoke with MVC Action" />
- <button type="button" onclick="return helloService();">Invoke with AJAX</button>
}
@if (@ViewBag.Response != null)
@@ -36,6 +35,11 @@
</div>
</text>
}
+
+ <div>
+ <button type="button" onclick="return helloService();">Invoke with AJAX</button>
+ </div>
+
</p>
</div>
</section>
@@ -73,7 +77,12 @@
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { name: "Passed Name" },
- success: function (data) { alert(data.Result); }
+ success: function (data) { alert(data.Result); },
+ error: function (xhr) {
+ if (xhr.status === 401) {
+ alert('You should be authenticated to call service.');
+ }
+ }
});
}
</script>

0 comments on commit d3c135f

Please sign in to comment.
Something went wrong with that request. Please try again.