C# 如何使用ASP.NET标识进行临时管理员访问[即将到来的页面]
我正在创建一个新网站,它已经在线,但我想阻止每个人访问!让我的客户每天使用它来检查开发过程,这样他就不需要下载所有东西并在计算机上进行设置 我想知道如何使用ASP身份,是否有任何简单的“黑客”来实现这一点 或者我唯一的办法是:C# 如何使用ASP.NET标识进行临时管理员访问[即将到来的页面],c#,asp.net,authorization,asp.net-identity,temporary,C#,Asp.net,Authorization,Asp.net Identity,Temporary,我正在创建一个新网站,它已经在线,但我想阻止每个人访问!让我的客户每天使用它来检查开发过程,这样他就不需要下载所有东西并在计算机上进行设置 我想知道如何使用ASP身份,是否有任何简单的“黑客”来实现这一点 或者我唯一的办法是: 将注册页面添加到网站 以管理员角色注册帐户 删除网站的注册页面 始终使用同一帐户检查页面 等待答案,如何简单轻松地实现这一点 编辑:也许有一种方法可以创建一个默认用户名(在本例中是我的管理员?) 谢谢大家!! 多谢各位 您需要它有多安全?你可以用另一种方式,通过IP进行限
多谢各位 您需要它有多安全?你可以用另一种方式,通过IP进行限制 选项1-仅允许特定IP访问您的站点 我建议您在web.config中阻止对该站点的所有访问,并且只将您和客户端连接的IP列为白名单-这将节省您的时间,并确保您只从您信任的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>
根据需要添加尽可能多的
指令
选项2-将基本身份验证添加到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>
这不是一个永久的解决办法。长期研究其他身份验证方法
<system.web>
<authentication mode="Forms">
<credentials passwordFormat="Clear">
<user name="jdoe1" password="someinsecurepassword" />
</credentials>
</authentication>
<authorization>
<allow users="jdoe1" />
<deny users="*" />
</authorization>
</system.web>
您需要它有多安全?你可以用另一种方式,通过IP进行限制 选项1-仅允许特定IP访问您的站点 我建议您在web.config中阻止对该站点的所有访问,并且只将您和客户端连接的IP列为白名单-这将节省您的时间,并确保您只从您信任的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>
根据需要添加尽可能多的
指令
选项2-将基本身份验证添加到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>
这不是一个永久的解决办法。长期研究其他身份验证方法
<system.web>
<authentication mode="Forms">
<credentials passwordFormat="Clear">
<user name="jdoe1" password="someinsecurepassword" />
</credentials>
</authentication>
<authorization>
<allow users="jdoe1" />
<deny users="*" />
</authorization>
</system.web>
因此,我通过一个特定的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; }
}
}
最后,我不得不在Web.config中添加以下内容:
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);
}
}
}
<?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>
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();
}
我希望它能帮助其他人;)
祝你好运 因此,我使用一个特定的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; }
}
}
最后,我不得不在Web.config中添加以下内容:
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);
}
}
}
<?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>
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();
}
我希望它能帮助其他人;)
祝你好运 正如user@trailmax建议的那样,我没有选择插入默认用户作为标识的最佳位置,因为每次调用上下文的构造函数时,我都会插入用户 经过分析,这当然不是一个好选项,因为每次访问数据库集时,我都使用上下文构造函数,无需触发对角色和用户数据库集的额外sql查询(因为我只需要在启动时使用该查询来插入不存在的用户) 所以。。由于我的web托管计划不允许任何更新迁移,也不允许从Package Manager控制台()运行种子方法,因此我想出了以下解决方案: 1) 我在远程mssql数据库上创建了所有运行sql脚本的表(要从上下文和模型中获取生成的sql脚本,我在包管理器控制台中键入:
Update-Database -Script -SourceMigration:0
2) 然后,我需要将数据库初始值设定项设置为null,这样EF就不会尝试删除/创建数据库,最后我只需从全局调用上下文类上的一个方法。方法应用程序启动中的asax:
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);
}
}
}
<?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>
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();
}
受保护的无效应用程序\u Start()
{
RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Database.SetInitializer(null);
新建MyContext().CreateDefaultUsers();
}
3) 这是我的方法,只在应用程序启动时调用(我几乎可以肯定,至少我希望如此,我想节省CPU时钟!)
公共类MyContext:IdentityDbContext
{
public MyContext():base(“MyContext”)
{
}
内部无效CreateDefaultUsers()
{
如果(!Roles.Any(r=>r.Name==“admin”))
{
var RoleManager=新RoleManager(新RoleStore(本));
RoleManager.Create(新的IdentityRole(“管理员”));
}
如果(!Users.Any(u=>u.UserName==“myadmin”))
{
var store=新用户存储(此);
var-manager=新用户管理器(存储);
var user=newidentityuser{UserName=“myadmin”};
创建(用户“mysupersafepwlulz”);
manager.AddToRole(user.Id,“admin”);
}
}
//更多的东西不适合这个问题。。。
}
我希望这对其他人有所帮助,谢谢@trailmax 正如用户@trailmax所说,我没有选择