使用ASP.NET身份创建默认用户帐户我正在使用ASP.NETMVC5和身份API开发一个简单的应用程序。请问如何在建表和启动应用的时候创建一个superadmin角色的superadmin账号。我在VisualStudio2013中使用默认的ASP.NETMVC5个人帐户模板,但我不知道如何实现我的目标。没关系,我发现这个有用的链接显示了我想要的内容:https://github.com/rustd/AspnetIdentitySample/blob/master/AspnetIdentitySample/App_Start/IdentityConfig.cs我在配置类的seed()方法中执行此操作配置如下:以上为C#学习教程:使用ASP.NETIdenity创建默认用户账号分享所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——protectedoverridevoidSeed(AuthContextcontext){conststringuserName="AdminUserName";conststringpassword="AdminPassword";conststringroleName="AdminRole";conststringphoneNumber="PhoneNumber";varappUserStore=newUserStore(context);用户管理器(appUserStore);varappRoleManager=newApplicationRoleManager(newRoleStore(context));尝试{varuser=context.Users.SingleOrDefault(u=>u.UserName==userName);varrole=context.Roles.SingleOrDefault(r=>r.Name==roleName);if(role==null){appRoleManager.CreateAsync(newIdentityRole(roleName)).Wait();role=context.Roles.SingleOrDefault(r=>r.Name==roleName);}如果(用户==null){appUserManager.CreateAsync(newApplicationUser{UserName=userName,PhoneNumber=phoneNumber},password).Wait();user=context.Users.SingleOrDefault(u=>u.UserName==nationalCode);}varuserRole=user.Roles.SingleOrDefault(r=>r.RoleId==role.Id);如果(userRole==null){appUserManager.AddToRoleAsync(user.Id,roleName).Wait();}}catch(Exceptionex){//错误捕获!}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
