如何使用ASP.NET身份进行临时管理员访问[即将推出的页面] [英] How to use ASP.NET Identity for Temporary Administrator Access [Coming-Soon-Page]

查看:105
本文介绍了如何使用ASP.NET身份进行临时管理员访问[即将推出的页面]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新网站,它已经在线,但是我想阻止所有人访问!而且,只要让我的客户每天使用它来检查开发过程,就不必下载所有内容并在计算机上进行设置.

我想知道如何使用ASP Identity,是否有任何简单的"hack"来实现这一目标?

或者我唯一的方法是:

  1. 将注册页面添加到网站
  2. 注册具有管理员角色的帐户
  3. 删除网站的注册页面
  4. 使用始终相同的帐户来查看页面

等待答案,如何简单轻松地实现这一目标?

也许有一种方法可以创建默认用户名(在这种情况下将是我的管理员?)

谢谢! 非常感谢你!

解决方案

由于@trailmax用户的建议,我并未选择为Identity插入默认用户的最佳位置,因为我每次都在上下文的构造函数中插入用户被叫.

经过分析后,这当然不是一个好选择,因为每次访问我的DbSet时,我都会使用上下文构造函数,而无需触发对Roles和Users DbSets的其他sql查询(因为我只需要在启动时插入该查询即可)用户(如果不存在).

...因为我的网络托管计划不允许任何更新迁移,也无法从Package Manager控制台运行种子方法(),所以我想出了以下解决方案:

1)我创建了所有在远程mssql数据库上运行sql脚本的表(要从上下文和模型中获取生成的SQL脚本,请键入

我希望这对外面的人有帮助,谢谢@trailmax!

I am creating a new website, and it's already online, but I want to block access to everyone! And just let my client use it to check the development process everyday, so he don't need to download everything and set up on his computer..

I was wondering about using ASP Identity, is there any simple "hack" to achieve this?

Or my only way is the following:

  1. Add the Register Page to the WebSite
  2. Register an account with Administrator role
  3. Delete Register Page of the WebSite
  4. Use always the same account to check the page

Waiting for an answer, how to simply and easily achieve this?

EDIT: Maybe there is a way to create a default username (that in this case will be my Administrator?)

Thank you! Thank you very much!

解决方案

As the user @trailmax suggested I was not choosing the best place to insert default users for Identity, since I was inserting the users each time the constructor of the context was called.

After analysis of course that's not a good option, because every time I access my DbSets I use the context constructor, triggering additional sql queries to Roles and Users DbSets, without need (because I only need that query on the startup to insert the users if they don't exist).

So.. since my web-hosting plan don't allow any Update Migrations neither Seed Methods to be runnable from Package Manager Console (because it always reference the 'master' database which I don't have permissions), I figured out this solution:

1) I create all my tables running a sql script on my remote mssql database (to get the generated SQL Script from my contexts and models, I type the following command in Package Manager Console:

Update-Database -Script -SourceMigration:0

2) Then I need to set Database Initializer to null, so EF doesn't try to drop/create a Database and finnaly I just invoke a method on my Context Class from my Global.asax in the method 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) And here is my method, that is only called on Application Startup (I am almost sure about it, at least I hope so, I wanna save CPU Clocks!)

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...

}

I hope this helps someone out there, and thank you @trailmax !

这篇关于如何使用ASP.NET身份进行临时管理员访问[即将推出的页面]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆