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

若如初见. 提交于 2020-01-06 02:54:08

问题


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.


回答1:


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");
}



回答2:


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)




回答3:


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.



来源:https://stackoverflow.com/questions/434776/how-do-i-add-first-user-root-in-asp-net-mvc-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!