2016-09-28 50 views
0

我正在創建一個新網站,它已經在線,但我想阻止訪問所有人!只是讓我的客戶用它來檢查每天的開發過程,所以他不需要下載所有東西,並在他的電腦上設置。如何使用ASP.NET身份進行臨時管理員訪問[即將推出]

我想知道使用ASP身份,有沒有簡單的「黑客」爲了達成這個?

還是我唯一的辦法是:

  1. 添加註冊頁面的網站
  2. 與管理員角色註冊一個帳號
  3. 刪除網站
  4. 使用的註冊頁面總是相同帳戶檢查頁面

等待答案,如何簡單和容易地實現這一點?

編輯:(?在這種情況下將是我的管理員)也許有創建一個默認的用戶名的方式

謝謝! 非常感謝!

+1

你是作爲個人或公司的一部分與基礎設施安裝?您可以輕鬆地在您的公司內部託管它,並通過VPN訪問您的網絡以訪問該站點 – techspider

+0

我正在以個人身份與我自己的基礎架構設置一起工作。我想我已經知道如何做到這一點,謝謝。 – TiagoM

回答

2

正如用戶@trailmax所示,我沒有選擇插入默認用戶的最佳位置,因爲每次調用上下文的構造函數時都會插入用戶。

經過分析當然這不是一個好的選擇,因爲每次我訪問我的DbSets時,我都使用上下文構造函數,而不需要觸發額外的SQL查詢給角色和用戶DbSets(因爲我只需要啓動時的查詢插入用戶,如果他們不存在)。

所以..因爲我的網站託管計劃不允許任何更新遷移既不種子的方法是從可運行程序包管理器控制檯(because it always reference the 'master' database which I don't have permissions),我想通了這個解決方案:

1)我創造我的全部運行在我的遠程MSSQL數據庫的SQL腳本表(從我的背景和模型獲取生成的SQL腳本,我在包管理器控制檯鍵入following command

Update-Database -Script -SourceMigration:0 

2)然後,我需要設置數據庫初始化器爲空,所以EF不會嘗試滴/創建一個數據庫和finnaly我只是在方法調用一個方法我上下文類從我的Global.asax的Application_Start

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
    Database.SetInitializer<MyContext>(null); 
    new MyContext().CreateDefaultUsers(); 
} 

3)這裏是我的方法,即只呼籲應用程序啓動(我幾乎可以肯定它,至少我希望如此,我想救CPU時鐘!)

public class MyContext : IdentityDbContext<IdentityUser> 
{ 
     public MyContext() : base("MyContext") 
     { 
     } 


     internal void CreateDefaultUsers() 
     { 
      if (!Roles.Any(r => r.Name == "admin")) 
      { 
       var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this)); 
       RoleManager.Create(new IdentityRole("admin")); 
      } 


      if (!Users.Any(u => u.UserName == "myadmin")) 
      { 
       var store = new UserStore<IdentityUser>(this); 
       var manager = new UserManager<IdentityUser>(store); 

       var user = new IdentityUser { UserName = "myadmin" }; 

       manager.Create(user, "mysupersafepwlulz"); 
       manager.AddToRole(user.Id, "admin"); 
      } 
     } 
    //More stuff not relevang for the question... 

} 

我希望這有助於有人出來,並感謝您@trailmax!

+1

看起來更好! – trailmax

0

你需要這個安全嗎?你可以通過限制IP來以其他方式進行。

選項1 - 只允許你的站點特定IP,

我建議阻斷你的web.config所有訪問該網站,只有白名單的IP(S),你和客戶端會從 - 連接 - 它會節省您的時間,它可以確保您只能從您信任的IP獲取連接。

修改相應的web.config

<configuration> 
    <system.webServer> 
    <security> 
     <ipSecurity allowUnlisted="false">      
     <clear/> 
     <add ipAddress="127.0.0.1" allowed="true"/> 
     <add ipAddress="x.x.x.x" allowed="true"/> <!-- your internal or external ip --> 
     <add ipAddress="x.x.x.x" allowed="true"/> <!-- your client's ip --> 
     <add ipAddress="a.b.c.0" subnetMask="255.255.255.0" allowed="true"/> <!-- your IP range (or client's IP range), if connecting from multpipe IPs on same subnet --> 
     </ipSecurity> 
    </security> 
    </configuration> 
</system.webServer> 

需要

添加儘可能多的 <add>指令

選項2 - 添加基本身份驗證的Web.config

這不是一個永久性的解決方案。長期考慮其他認證方法。

<system.web>  
    <authentication mode="Forms">  
    <credentials passwordFormat="Clear">  
     <user name="jdoe1" password="someinsecurepassword" />  
    </credentials>  
    </authentication>  
    <authorization>  
    <allow users="jdoe1" />  
    <deny users="*" />  
    </authorization>  
</system.web>  
+0

我再次重申,這不應該是一個永久的解決方案,而只是一個「黑客」。 – bitwisebytefoolish

+0

感謝您的答案,但使用身份驗證我需要做一個新的表格,我正在使用MVC ..另一種選擇使用IP它目前不適合我,因爲他們的IP將一直在變化。 – TiagoM

0

所以我設法這與特定的DbContext創建我的默認用戶如果他沒有在數據庫中。

這裏是跟進代碼:

namespace MyNamespace 
{ 
    public class MyContext: IdentityDbContext<IdentityUser> 
     { 

      public MyContext() : base("DefaultConnection") 
      { 
       if (!Roles.Any(r => r.Name == "admin")) 
       { 
        var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this)); 
        RoleManager.Create(new IdentityRole("admin")); 
       }     


       if (!Users.Any(u => u.UserName == "mydefaultuser")) 
       { 
        var store = new UserStore<IdentityUser>(this); 
        var manager = new UserManager<IdentityUser>(store); 

        var user = new IdentityUser { UserName = "mydefaultuser" }; 

        manager.Create(user, "password"); 
        manager.AddToRole(user.Id, "admin"); 
       } 
      } 

      protected override void OnModelCreating(DbModelBuilder modelBuilder) 
      { 
       base.OnModelCreating(modelBuilder); 
       modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
      } 

      public DbSet<MyItem1> myStuff1 { get; set; } 
      public DbSet<MyItem2> myStuff2 { get; set; } 
      public DbSet<MyItem3> myStuff3 { get; set; } 
      public DbSet<MyItem4> myStuff4 { get; set; } 
      public DbSet<MyItem5> myStuff5 { get; set; } 
     } 
} 

然後我需要指定配置類爲身份創建該類的App_Start

namespace MyNamespace 
{ 
    public class IdentityConfig 
     { 
      public void Configuration(IAppBuilder app) 
      { 
       app.CreatePerOwinContext(() => new WeirdMachineContext()); 
       app.UseCookieAuthentication(new CookieAuthenticationOptions 
       { 
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
        LoginPath = new PathString("/Home/Login"), 
       }); 
       FormsAuthentication.SetAuthCookie("CookieValue", false); 
      } 
     } 
} 

而在去年我有在Web中添加以下內容。配置

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections></configSections> 
    <appSettings> 
    <!-- A lot of other stuff here.. --> 
    <add key="owin:AppStartup" value="MyNamespace.IdentityConfig" /> 
    </appSettings> 
    <!-- More stuff here.. --> 
</configuration> 

我希望它可以幫助人在那裏;)

祝你好運!

+1

創建用戶並且DbContext中的角色是迄今爲止我看到的最糟糕的解決方案 - 這意味着每次創建DbContext時都會觸發2個不必要的SQL請求,即每個HTTP請求都會創建一個DbContext。另外在構造函數中這樣做會導致死鎖。只要確保這是非常臨時的解決方案。 – trailmax

+1

看到這個答案:http://stackoverflow.com/a/39738064/809357 - 稍微好一點的地方,讓您的用戶創建 – trailmax

+0

這實際上是一個很好的方法,我沒有,雖然這一點,謝謝!如果你想用這個解決方案寫一個答案,並且在測試之後,我會很樂意接受它作爲解決方案! ;)謝謝@trailmax – TiagoM

相關問題