How to access AspNetUserRoles table in ASP.NET MVC 5
I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help
c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
add a comment |
I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help
c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
what have you tried so far?
– Simon Price
Jan 2 at 23:01
1
Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?
– Ghukas
Jan 2 at 23:07
Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas
– Alejandro1991
Jan 2 at 23:11
I did not try anything just i want to access to this table from DB object in Controller @SimonPrice
– Alejandro1991
Jan 2 at 23:14
AspNetUserRoles
table is a mapping table. You can access the table usingAspNetUser
object orAspNetRole
object. Assuming you want to show all users and their roles, you can do that by getting it fromvar userRoles = user.AspNetRoles;
for each of the users.
– Ghukas
Jan 3 at 2:32
add a comment |
I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help
c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help
c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
asked Jan 2 at 23:00


Alejandro1991Alejandro1991
397
397
what have you tried so far?
– Simon Price
Jan 2 at 23:01
1
Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?
– Ghukas
Jan 2 at 23:07
Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas
– Alejandro1991
Jan 2 at 23:11
I did not try anything just i want to access to this table from DB object in Controller @SimonPrice
– Alejandro1991
Jan 2 at 23:14
AspNetUserRoles
table is a mapping table. You can access the table usingAspNetUser
object orAspNetRole
object. Assuming you want to show all users and their roles, you can do that by getting it fromvar userRoles = user.AspNetRoles;
for each of the users.
– Ghukas
Jan 3 at 2:32
add a comment |
what have you tried so far?
– Simon Price
Jan 2 at 23:01
1
Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?
– Ghukas
Jan 2 at 23:07
Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas
– Alejandro1991
Jan 2 at 23:11
I did not try anything just i want to access to this table from DB object in Controller @SimonPrice
– Alejandro1991
Jan 2 at 23:14
AspNetUserRoles
table is a mapping table. You can access the table usingAspNetUser
object orAspNetRole
object. Assuming you want to show all users and their roles, you can do that by getting it fromvar userRoles = user.AspNetRoles;
for each of the users.
– Ghukas
Jan 3 at 2:32
what have you tried so far?
– Simon Price
Jan 2 at 23:01
what have you tried so far?
– Simon Price
Jan 2 at 23:01
1
1
Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?
– Ghukas
Jan 2 at 23:07
Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?
– Ghukas
Jan 2 at 23:07
Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas
– Alejandro1991
Jan 2 at 23:11
Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas
– Alejandro1991
Jan 2 at 23:11
I did not try anything just i want to access to this table from DB object in Controller @SimonPrice
– Alejandro1991
Jan 2 at 23:14
I did not try anything just i want to access to this table from DB object in Controller @SimonPrice
– Alejandro1991
Jan 2 at 23:14
AspNetUserRoles
table is a mapping table. You can access the table using AspNetUser
object or AspNetRole
object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles;
for each of the users.– Ghukas
Jan 3 at 2:32
AspNetUserRoles
table is a mapping table. You can access the table using AspNetUser
object or AspNetRole
object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles;
for each of the users.– Ghukas
Jan 3 at 2:32
add a comment |
2 Answers
2
active
oldest
votes
This will give you a list of all users and their roles.
using(var context = new YourContextName())
{
var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
var users = context.AspNetUsers;
foreach(var user in users)
{
foreach(var role in user.Roles)
{
usersAndRoles.Add(new UserRoleModel
{
UserName = user.UserName,
RoleName = role.Name
};
}
}
}
I haven't tested the code, there can be a more optimal way.
thanks alot this way is nice @Ghukas
– Alejandro1991
Jan 3 at 14:46
add a comment |
You need to import the following model on the view where you want to retrieve the User Roles.
@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014290%2fhow-to-access-aspnetuserroles-table-in-asp-net-mvc-5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This will give you a list of all users and their roles.
using(var context = new YourContextName())
{
var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
var users = context.AspNetUsers;
foreach(var user in users)
{
foreach(var role in user.Roles)
{
usersAndRoles.Add(new UserRoleModel
{
UserName = user.UserName,
RoleName = role.Name
};
}
}
}
I haven't tested the code, there can be a more optimal way.
thanks alot this way is nice @Ghukas
– Alejandro1991
Jan 3 at 14:46
add a comment |
This will give you a list of all users and their roles.
using(var context = new YourContextName())
{
var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
var users = context.AspNetUsers;
foreach(var user in users)
{
foreach(var role in user.Roles)
{
usersAndRoles.Add(new UserRoleModel
{
UserName = user.UserName,
RoleName = role.Name
};
}
}
}
I haven't tested the code, there can be a more optimal way.
thanks alot this way is nice @Ghukas
– Alejandro1991
Jan 3 at 14:46
add a comment |
This will give you a list of all users and their roles.
using(var context = new YourContextName())
{
var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
var users = context.AspNetUsers;
foreach(var user in users)
{
foreach(var role in user.Roles)
{
usersAndRoles.Add(new UserRoleModel
{
UserName = user.UserName,
RoleName = role.Name
};
}
}
}
I haven't tested the code, there can be a more optimal way.
This will give you a list of all users and their roles.
using(var context = new YourContextName())
{
var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
var users = context.AspNetUsers;
foreach(var user in users)
{
foreach(var role in user.Roles)
{
usersAndRoles.Add(new UserRoleModel
{
UserName = user.UserName,
RoleName = role.Name
};
}
}
}
I haven't tested the code, there can be a more optimal way.
answered Jan 3 at 2:51
GhukasGhukas
423922
423922
thanks alot this way is nice @Ghukas
– Alejandro1991
Jan 3 at 14:46
add a comment |
thanks alot this way is nice @Ghukas
– Alejandro1991
Jan 3 at 14:46
thanks alot this way is nice @Ghukas
– Alejandro1991
Jan 3 at 14:46
thanks alot this way is nice @Ghukas
– Alejandro1991
Jan 3 at 14:46
add a comment |
You need to import the following model on the view where you want to retrieve the User Roles.
@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.
add a comment |
You need to import the following model on the view where you want to retrieve the User Roles.
@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.
add a comment |
You need to import the following model on the view where you want to retrieve the User Roles.
@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.
You need to import the following model on the view where you want to retrieve the User Roles.
@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>
Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.
answered Jan 2 at 23:29


Didac RiusDidac Rius
363
363
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014290%2fhow-to-access-aspnetuserroles-table-in-asp-net-mvc-5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what have you tried so far?
– Simon Price
Jan 2 at 23:01
1
Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?
– Ghukas
Jan 2 at 23:07
Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas
– Alejandro1991
Jan 2 at 23:11
I did not try anything just i want to access to this table from DB object in Controller @SimonPrice
– Alejandro1991
Jan 2 at 23:14
AspNetUserRoles
table is a mapping table. You can access the table usingAspNetUser
object orAspNetRole
object. Assuming you want to show all users and their roles, you can do that by getting it fromvar userRoles = user.AspNetRoles;
for each of the users.– Ghukas
Jan 3 at 2:32