Commits

Clément Bourgeois committed ed054ff

New FakeMembershipAuthHttpModule for localhost setups.

  • Participants
  • Parent commits b0518b2

Comments (0)

Files changed (4)

SQLServerBackupTool.Web/Lib/BasicMembershipAuthHttpModule.cs

-using System;
+using SQLServerBackupTool.Lib.Annotations;
+using System;
 using System.Data;
 using System.Data.Entity.Infrastructure;
 using System.Net.Http.Headers;
 
 namespace SQLServerBackupTool.Web.Lib
 {
+    [UsedImplicitly]
     public class BasicMembershipAuthHttpModule : IHttpModule
     {
         private static string _realm = "Access restricted area";

SQLServerBackupTool.Web/Lib/FakeMembershipAuthHttpModule.cs

+using SQLServerBackupTool.Lib.Annotations;
+using System;
+using System.Security.Principal;
+using System.Threading;
+using System.Web;
+
+namespace SQLServerBackupTool.Web.Lib
+{
+    [UsedImplicitly]
+    public class FakeMembershipAuthHttpModule : IHttpModule
+    {
+        private static readonly GenericPrincipal FakePrincipal = new GenericPrincipal(new GenericIdentity("admin"), new[] { "Admin" });
+
+        public void Init(HttpApplication context)
+        {
+            context.AuthenticateRequest += OnApplicationAuthenticateRequest;
+        }
+
+        private static void SetPrincipal(IPrincipal principal)
+        {
+            Thread.CurrentPrincipal = principal;
+
+            if (HttpContext.Current != null)
+            {
+                HttpContext.Current.User = principal;
+            }
+        }
+
+        private static void OnApplicationAuthenticateRequest(object sender, EventArgs e)
+        {
+            SetPrincipal(FakePrincipal);
+        }
+
+        public void Dispose()
+        {
+
+        }
+    }
+}

SQLServerBackupTool.Web/SQLServerBackupTool.Web.csproj

   <ItemGroup>
     <Compile Include="App_Start\AutoInstall.cs" />
     <Compile Include="App_Start\BundleConfig.cs" />
+    <Compile Include="Lib\FakeMembershipAuthHttpModule.cs" />
     <Compile Include="Lib\BackupsManager.cs" />
     <Compile Include="Lib\Extensions\NumberExtensions.cs" />
     <Compile Include="Lib\Mvc\FlashMessages.cs" />

SQLServerBackupTool.Web/Web.config

       <add name="Api" path="api/*" verb="*" type="System.Web.Routing.UrlRoutingHandler, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" preCondition="integratedMode,runtimeVersionv4.0" />
     </handlers>
     <modules>
+      <!-- Basic HTTP Auth -->
       <add name="BasicMembershipAuthHttpModule" type="SQLServerBackupTool.Web.Lib.BasicMembershipAuthHttpModule" />
+      <!-- Fake admin Auth -->
+      <!--<add name="FakeMembershipAuthHttpModule" type="SQLServerBackupTool.Web.Lib.FakeMembershipAuthHttpModule"/>-->
     </modules>
   </system.webServer>
   <entityFramework>