How to implement permission based authorization in ASP.net core Identity?
I am trying to secure my webAPI using asp.net Identity Core. Now I want to create Roles Dynamically and set and remove permission from/to them and in my admin panel.
for example, I have this permissions list:
- register task
- assign task
- change task status
- verify task status
now I want to create different roles and set this permission to them as my needs and assign these roles to each user.
I searched in UserManager and RoleManager of Identity framework but there was no way to create this functionality.
is there any method for implementing this functionality?
I find this useful but this is about dotnet
c# asp.net-web-api asp.net-core asp.net-identity
add a comment |
I am trying to secure my webAPI using asp.net Identity Core. Now I want to create Roles Dynamically and set and remove permission from/to them and in my admin panel.
for example, I have this permissions list:
- register task
- assign task
- change task status
- verify task status
now I want to create different roles and set this permission to them as my needs and assign these roles to each user.
I searched in UserManager and RoleManager of Identity framework but there was no way to create this functionality.
is there any method for implementing this functionality?
I find this useful but this is about dotnet
c# asp.net-web-api asp.net-core asp.net-identity
Possible duplicate of How to create roles in asp.net core and assign them to users
– Ian Kemp
Dec 30 '18 at 19:43
No this is not. I am searching for permission base authorization but in your link, it is about role-based authorization and specifically about creating roles.
– Navid_pdp11
Dec 31 '18 at 5:34
add a comment |
I am trying to secure my webAPI using asp.net Identity Core. Now I want to create Roles Dynamically and set and remove permission from/to them and in my admin panel.
for example, I have this permissions list:
- register task
- assign task
- change task status
- verify task status
now I want to create different roles and set this permission to them as my needs and assign these roles to each user.
I searched in UserManager and RoleManager of Identity framework but there was no way to create this functionality.
is there any method for implementing this functionality?
I find this useful but this is about dotnet
c# asp.net-web-api asp.net-core asp.net-identity
I am trying to secure my webAPI using asp.net Identity Core. Now I want to create Roles Dynamically and set and remove permission from/to them and in my admin panel.
for example, I have this permissions list:
- register task
- assign task
- change task status
- verify task status
now I want to create different roles and set this permission to them as my needs and assign these roles to each user.
I searched in UserManager and RoleManager of Identity framework but there was no way to create this functionality.
is there any method for implementing this functionality?
I find this useful but this is about dotnet
c# asp.net-web-api asp.net-core asp.net-identity
c# asp.net-web-api asp.net-core asp.net-identity
edited Jan 1 at 14:48
Ian Kemp
16.7k126898
16.7k126898
asked Dec 30 '18 at 11:53
Navid_pdp11Navid_pdp11
90311230
90311230
Possible duplicate of How to create roles in asp.net core and assign them to users
– Ian Kemp
Dec 30 '18 at 19:43
No this is not. I am searching for permission base authorization but in your link, it is about role-based authorization and specifically about creating roles.
– Navid_pdp11
Dec 31 '18 at 5:34
add a comment |
Possible duplicate of How to create roles in asp.net core and assign them to users
– Ian Kemp
Dec 30 '18 at 19:43
No this is not. I am searching for permission base authorization but in your link, it is about role-based authorization and specifically about creating roles.
– Navid_pdp11
Dec 31 '18 at 5:34
Possible duplicate of How to create roles in asp.net core and assign them to users
– Ian Kemp
Dec 30 '18 at 19:43
Possible duplicate of How to create roles in asp.net core and assign them to users
– Ian Kemp
Dec 30 '18 at 19:43
No this is not. I am searching for permission base authorization but in your link, it is about role-based authorization and specifically about creating roles.
– Navid_pdp11
Dec 31 '18 at 5:34
No this is not. I am searching for permission base authorization but in your link, it is about role-based authorization and specifically about creating roles.
– Navid_pdp11
Dec 31 '18 at 5:34
add a comment |
1 Answer
1
active
oldest
votes
I find an approach which is using claim and policy for creating a permission-based authorization in this link.
I create a custom claim type such as Application.Permission and then create some classes as following to define my permissions:
public class CustomClaimTypes
{
public const string Permission = "Application.Permission";
}
public static class UserPermissions
{
public const string Add = "users.add";
public const string Edit = "users.edit";
public const string EditRole = "users.edit.role";
}
and then I create My roles and then Assign these permissions as claims to the roles with key ApplicationPermission.
await roleManager.CreateAsync(new ApplicationRole("User"));
var userRole = await roleManager.FindByNameAsync("User");
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.User.View));
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.Team.View));
in the next step, I add these claims to my token when the user is trying to login to system:
var roles = await _userManager.GetRolesAsync(user);
var userRoles = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToArray();
var userClaims = await _userManager.GetClaimsAsync(user).ConfigureAwait(false);
var roleClaims = await GetRoleClaimsAsync(roles).ConfigureAwait(false);
var claims = new
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.UserName)
}.Union(userClaims).Union(roleClaims).Union(userRoles);
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SigningKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _jwtSettings.Issuer,
audience: _jwtSettings.Audience,
claims: claims,
expires: DateTime.UtcNow.AddYears(1),
signingCredentials: creds);
then I create my policies this way:
public static class PolicyTypes
{
public static class Users
{
public const string Manage = "users.manage.policy";
public const string EditRole = "users.edit.role.policy";
}
}
then I set up my authorization service inside startup.cs file in ConfigureServiceSection:
services.AddAuthorization(options =>
{
options.AddPolicy(PolicyTypes.Users.Manage, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.Add); });
options.AddPolicy(PolicyTypes.Users.EditRole, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.EditRole); });
}
finally, I set policies on my routes and finish:
[Authorize(Policy = PolicyTypes.Users.Manage)]
public async Task<IEnumerable<TeamDto>> GetSubTeams(int parentId)
{
var teams = await _teamService.GetSubTeamsAsync(parentId);
return teams;
}
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%2f53977356%2fhow-to-implement-permission-based-authorization-in-asp-net-core-identity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I find an approach which is using claim and policy for creating a permission-based authorization in this link.
I create a custom claim type such as Application.Permission and then create some classes as following to define my permissions:
public class CustomClaimTypes
{
public const string Permission = "Application.Permission";
}
public static class UserPermissions
{
public const string Add = "users.add";
public const string Edit = "users.edit";
public const string EditRole = "users.edit.role";
}
and then I create My roles and then Assign these permissions as claims to the roles with key ApplicationPermission.
await roleManager.CreateAsync(new ApplicationRole("User"));
var userRole = await roleManager.FindByNameAsync("User");
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.User.View));
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.Team.View));
in the next step, I add these claims to my token when the user is trying to login to system:
var roles = await _userManager.GetRolesAsync(user);
var userRoles = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToArray();
var userClaims = await _userManager.GetClaimsAsync(user).ConfigureAwait(false);
var roleClaims = await GetRoleClaimsAsync(roles).ConfigureAwait(false);
var claims = new
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.UserName)
}.Union(userClaims).Union(roleClaims).Union(userRoles);
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SigningKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _jwtSettings.Issuer,
audience: _jwtSettings.Audience,
claims: claims,
expires: DateTime.UtcNow.AddYears(1),
signingCredentials: creds);
then I create my policies this way:
public static class PolicyTypes
{
public static class Users
{
public const string Manage = "users.manage.policy";
public const string EditRole = "users.edit.role.policy";
}
}
then I set up my authorization service inside startup.cs file in ConfigureServiceSection:
services.AddAuthorization(options =>
{
options.AddPolicy(PolicyTypes.Users.Manage, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.Add); });
options.AddPolicy(PolicyTypes.Users.EditRole, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.EditRole); });
}
finally, I set policies on my routes and finish:
[Authorize(Policy = PolicyTypes.Users.Manage)]
public async Task<IEnumerable<TeamDto>> GetSubTeams(int parentId)
{
var teams = await _teamService.GetSubTeamsAsync(parentId);
return teams;
}
add a comment |
I find an approach which is using claim and policy for creating a permission-based authorization in this link.
I create a custom claim type such as Application.Permission and then create some classes as following to define my permissions:
public class CustomClaimTypes
{
public const string Permission = "Application.Permission";
}
public static class UserPermissions
{
public const string Add = "users.add";
public const string Edit = "users.edit";
public const string EditRole = "users.edit.role";
}
and then I create My roles and then Assign these permissions as claims to the roles with key ApplicationPermission.
await roleManager.CreateAsync(new ApplicationRole("User"));
var userRole = await roleManager.FindByNameAsync("User");
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.User.View));
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.Team.View));
in the next step, I add these claims to my token when the user is trying to login to system:
var roles = await _userManager.GetRolesAsync(user);
var userRoles = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToArray();
var userClaims = await _userManager.GetClaimsAsync(user).ConfigureAwait(false);
var roleClaims = await GetRoleClaimsAsync(roles).ConfigureAwait(false);
var claims = new
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.UserName)
}.Union(userClaims).Union(roleClaims).Union(userRoles);
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SigningKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _jwtSettings.Issuer,
audience: _jwtSettings.Audience,
claims: claims,
expires: DateTime.UtcNow.AddYears(1),
signingCredentials: creds);
then I create my policies this way:
public static class PolicyTypes
{
public static class Users
{
public const string Manage = "users.manage.policy";
public const string EditRole = "users.edit.role.policy";
}
}
then I set up my authorization service inside startup.cs file in ConfigureServiceSection:
services.AddAuthorization(options =>
{
options.AddPolicy(PolicyTypes.Users.Manage, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.Add); });
options.AddPolicy(PolicyTypes.Users.EditRole, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.EditRole); });
}
finally, I set policies on my routes and finish:
[Authorize(Policy = PolicyTypes.Users.Manage)]
public async Task<IEnumerable<TeamDto>> GetSubTeams(int parentId)
{
var teams = await _teamService.GetSubTeamsAsync(parentId);
return teams;
}
add a comment |
I find an approach which is using claim and policy for creating a permission-based authorization in this link.
I create a custom claim type such as Application.Permission and then create some classes as following to define my permissions:
public class CustomClaimTypes
{
public const string Permission = "Application.Permission";
}
public static class UserPermissions
{
public const string Add = "users.add";
public const string Edit = "users.edit";
public const string EditRole = "users.edit.role";
}
and then I create My roles and then Assign these permissions as claims to the roles with key ApplicationPermission.
await roleManager.CreateAsync(new ApplicationRole("User"));
var userRole = await roleManager.FindByNameAsync("User");
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.User.View));
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.Team.View));
in the next step, I add these claims to my token when the user is trying to login to system:
var roles = await _userManager.GetRolesAsync(user);
var userRoles = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToArray();
var userClaims = await _userManager.GetClaimsAsync(user).ConfigureAwait(false);
var roleClaims = await GetRoleClaimsAsync(roles).ConfigureAwait(false);
var claims = new
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.UserName)
}.Union(userClaims).Union(roleClaims).Union(userRoles);
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SigningKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _jwtSettings.Issuer,
audience: _jwtSettings.Audience,
claims: claims,
expires: DateTime.UtcNow.AddYears(1),
signingCredentials: creds);
then I create my policies this way:
public static class PolicyTypes
{
public static class Users
{
public const string Manage = "users.manage.policy";
public const string EditRole = "users.edit.role.policy";
}
}
then I set up my authorization service inside startup.cs file in ConfigureServiceSection:
services.AddAuthorization(options =>
{
options.AddPolicy(PolicyTypes.Users.Manage, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.Add); });
options.AddPolicy(PolicyTypes.Users.EditRole, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.EditRole); });
}
finally, I set policies on my routes and finish:
[Authorize(Policy = PolicyTypes.Users.Manage)]
public async Task<IEnumerable<TeamDto>> GetSubTeams(int parentId)
{
var teams = await _teamService.GetSubTeamsAsync(parentId);
return teams;
}
I find an approach which is using claim and policy for creating a permission-based authorization in this link.
I create a custom claim type such as Application.Permission and then create some classes as following to define my permissions:
public class CustomClaimTypes
{
public const string Permission = "Application.Permission";
}
public static class UserPermissions
{
public const string Add = "users.add";
public const string Edit = "users.edit";
public const string EditRole = "users.edit.role";
}
and then I create My roles and then Assign these permissions as claims to the roles with key ApplicationPermission.
await roleManager.CreateAsync(new ApplicationRole("User"));
var userRole = await roleManager.FindByNameAsync("User");
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.User.View));
await roleManager.AddClaimAsync(userRole, new Claim(CustomClaimTypes.Permission, Permissions.Team.View));
in the next step, I add these claims to my token when the user is trying to login to system:
var roles = await _userManager.GetRolesAsync(user);
var userRoles = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToArray();
var userClaims = await _userManager.GetClaimsAsync(user).ConfigureAwait(false);
var roleClaims = await GetRoleClaimsAsync(roles).ConfigureAwait(false);
var claims = new
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.UserName)
}.Union(userClaims).Union(roleClaims).Union(userRoles);
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SigningKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _jwtSettings.Issuer,
audience: _jwtSettings.Audience,
claims: claims,
expires: DateTime.UtcNow.AddYears(1),
signingCredentials: creds);
then I create my policies this way:
public static class PolicyTypes
{
public static class Users
{
public const string Manage = "users.manage.policy";
public const string EditRole = "users.edit.role.policy";
}
}
then I set up my authorization service inside startup.cs file in ConfigureServiceSection:
services.AddAuthorization(options =>
{
options.AddPolicy(PolicyTypes.Users.Manage, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.Add); });
options.AddPolicy(PolicyTypes.Users.EditRole, policy => { policy.RequireClaim(CustomClaimTypes.Permission, Permissions.Users.EditRole); });
}
finally, I set policies on my routes and finish:
[Authorize(Policy = PolicyTypes.Users.Manage)]
public async Task<IEnumerable<TeamDto>> GetSubTeams(int parentId)
{
var teams = await _teamService.GetSubTeamsAsync(parentId);
return teams;
}
answered Jan 1 at 13:30
Navid_pdp11Navid_pdp11
90311230
90311230
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%2f53977356%2fhow-to-implement-permission-based-authorization-in-asp-net-core-identity%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
Possible duplicate of How to create roles in asp.net core and assign them to users
– Ian Kemp
Dec 30 '18 at 19:43
No this is not. I am searching for permission base authorization but in your link, it is about role-based authorization and specifically about creating roles.
– Navid_pdp11
Dec 31 '18 at 5:34