How do I add first user (root) in ASP.NET MVC app?

2019-08-01 05:30发布

I'm using ASP.NET MVC Framwork and trying to grok the ASP Membership 3.5 stuff.

What is the best way to add the first administrator user without having to log in? I've been staring at the membership starter kit's source without finding it.

3条回答
一夜七次
2楼-- · 2019-08-01 05:58

A simple solution (especially or rather only for dev. purposes) by doing it in a "setup" action:

if (!Roles.RoleExists("Administrator"))
{
    Roles.CreateRole("Administrator");
}
if (Membership.GetUser("Admin") == null)
{
    Membership.CreateUser("Admin", "Admin");
    Roles.AddUserToRole("Admin", "Administrator");
}
查看更多
Deceive 欺骗
3楼-- · 2019-08-01 06:04

You said you were going to share the project. I would recommend creating a one-time-run page that creates the user and assigns it the roel you want. After that any concurrent runs of the page should than check for the user you want and if it exists to redirect away or some other mechanic.

查看更多
乱世女痞
4楼-- · 2019-08-01 06:09

Depends on what memebership provider your are using.. If your using a SQL memembershipprovider you should be able to use the regular Asp.Net Configuration tool. (Project menu -> Asp.Net Configuration)

查看更多
登录 后发表回答