В проекте используется стандартная аутентификация Individual User Accounts. Добавил программно роль:
var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "admin";
roleManager.Create(role);
UserManager.AddToRole(user.Id, "admin");
В БД соответствено появились две записи о ролях. Но при попытке на странице вывести роль текущего пользователя @Roles.GetRolesForUser() возникает ошибка:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Web.config:
<system.web>
<authentication mode="Forms">
<forms name="cookies" timeout="2880" loginUrl="/Account/Login" />
</authentication>
<roleManager enabled="true" />
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
</system.web>
upd. Ошибка возникает из-за @Roles.GetRolesForUser(). Написал свою функцию для определения роли:
string id =User.Identity.GetUserId();
var rolesUser = UserManager.GetRoles(id);
Продвижение своими сайтами как стратегия роста и независимости