How to call an "Admin" role in an IF in C#?

Asked

Viewed 47 times

0

Guys I created several roles for me to do an access control. So I want to know how to call the admin role so I can pass it in an if, like example I’ll put below in my _layout:

  @if ("Aqui que eu preciso saber como chamar a role admin")
                {
                    <li>@Html.ActionLink("Início", "Index", "Home")</li>
                    <li>@Html.ActionLink("Sobre", "About", "Home")</li>
                    <li>@Html.ActionLink("Contato", "Contact", "Home")</li>
                }
                else
                {
                    <li>@Html.ActionLink("Sobre", "About", "Home")</li>
                }

Here’s how I created the role in my startup archive:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        createRolesandUsers();
    }


    // In this method we will create default User roles and Admin user for login  
    private void createRolesandUsers()
    {
        ApplicationDbContext context = new ApplicationDbContext();

        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));


        // In Startup iam creating first Admin Role and creating a default Admin User   
        if (!roleManager.RoleExists("Admin"))
        {

            // criando a role do usuario Admin 
            var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
            role.Name = "Admin";
            roleManager.Create(role);

            //Aqui estou criando o usuario que terá o acesso completo                 

            var user = new ApplicationUser();
            user.UserName = "ebase";
            user.Email = "[email protected]";

            string userPWD = "123456";

            var chkUser = UserManager.Create(user, userPWD);

            //Add default User to Role Admin  
            if (chkUser.Succeeded)
            {
                var result1 = UserManager.AddToRole(user.Id, "Admin");

            }
        }

1 answer

1


@if (User.IsInRole("Admin"))
{

}
  • Passing the name Admin is not working, which would be the correct form? The menu still not displayed after logging in

  • User has admin permission in Aspnetuserroles table ? The admin role is registered under the same name in Aspnetusers ?

  • I don’t understand, I need to do something in the database???

  • Yes, you must have saved in the table Aspnetroles, the role id and the name, example: 1 - Admin E in the Aspnetusersroles table the user id and role id, example: **Userid | Roleid 2 | 1 **

  • Also check that the user is logged in

  • The ID of both tables should be equal then???

  • Yes, because the Roleid column is a foreign key. And its id must exist in Aspnetroles

  • I created a new user here so when I went to the table Aspnetuserroles was all null, only the data are saved in the table Aspnetusers and Aspnetroles, how do I save it in the other table? Sorry more is that I’m starting now with C#, before I was programming in PHP

  • Dude got it, I did what you said, I went to Aspnetuserroles and I put the ID and roleid the same as the others, so it worked

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.