如何使用Asp.Net Core身份扩展多个位置/站点的角色

时间:2019-05-12 21:37:19

标签: asp.net-core entity-framework-core asp.net-identity

我有一个在多站点/多位置环境中使用的API。目前,每个用户均已定义角色,但仅锁定在一个位置。我需要将其扩展到用户可能在一个位置具有管理员角色,然后可能在另一位置成为标准用户的位置。他们可能也没有角色/无法访问多个位置。

这是我现在正在使用的东西(asp.net core 2.2):

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? LocationId { get; set; }
    public virtual Locations Locations { get; set; }
    public int? ContactPersonId { get; set; }
    public virtual ContactPerson ContactPerson { get; set; }
}

public class Locations
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ContactPerson
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string UserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

    public virtual ICollection<ContactOrganizationPerson> ContactOrganizationPeople { get; set; }
    public virtual ICollection<ContactAddress> ContactAddresses { get; set; }
    public virtual ICollection<ContactPhone> ContactPhones { get; set; }
    public virtual ICollection<ContactEmail> ContactEmails { get; set; }

}

我打算将ApplicationUser to Locations表关系更改为“多对多”关系,这会将用户链接到允许访问的Locations。尽管我已经将有效负载放置在M2M关系表中,该有效负载将指定UserId,LocationId和Roles,但我宁愿让Identity在可能的情况下进行处理。

是否可以扩展AspNetUserRoles,以便为每个位置指定用户到角色关系?还是有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

我不确定这是否会对您有所帮助,但我已经扩展了.Net Core并具有IAuthorizationRequirement功能。

#Software: Microsoft Internet Information Services 7.0
#Version: 1.0
#Date: 2019-05-14 15:58:17
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status time-taken
2019-05-14 15:58:17 10.100.10.00 GET /browsercheck - 80 - 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 401 2 5 72
2019-05-14 15:58:17 10.100.10.00 GET /browsercheck - 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 301 0 0 3
2019-05-14 15:58:20 10.100.10.00 GET /browsercheck/ - 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 200 0 0 2349
2019-05-14 15:58:20 10.100.10.0 GET /BrowserCheck/bundles/MsAjaxJs v=VA_FXLaB5PurewZl92JsrSUQcDrqhwBct539oVLEeiY1 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 404 0 2 2
2019-05-14 15:58:20 10.100.10.00 GET /BrowserCheck/bundles/WebFormsJs v=N8tymL9KraMLGAMFuPycfH3pXe6uUlRXdhtYv8A_jUU1 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 404 0 2 2
2019-05-14 15:58:22 10.100.10.00 POST /browsercheck/ - 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 200 0 0 108
2019-05-14 15:58:22 10.100.10.0 GET /BrowserCheck/bundles/MsAjaxJs v=VA_FXLaB5PurewZl92JsrSUQcDrqhwBct539oVLEeiY1 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 404 0 2 2
2019-05-14 15:58:22 10.100.10.00 GET /BrowserCheck/bundles/WebFormsJs v=N8tymL9KraMLGAMFuPycfH3pXe6uUlRXdhtYv8A_jUU1 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko 404 0 2 2
2019-05-14 15:58:37 10.100.10.00 GET /browsercheck/ - 80 - 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/74.0.3729.131+Safari/537.36 401 2 5 0
2019-05-14 15:58:37 10.100.10.00 GET /browsercheck/ - 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/74.0.3729.131+Safari/537.36 500 0 0 76
2019-05-14 15:58:37 10.100.10.00 GET /favicon.ico - 80 - 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/74.0.3729.131+Safari/537.36 401 2 5 98
2019-05-14 15:58:37 10.100.10.00 GET /favicon.ico - 80 ABC\user1 00.000.00.00 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/74.0.3729.131+Safari/537.36 404 0 2 3

创建一个新的班级

public class CustomRequirement : IAuthorizationRequirement
{
    public CustomRequirement ()
    {
    }
}

覆盖HandleRequirementAsync

public class CustomHandler : AuthorizationHandler<CustomRequirement>

在这里您可以从数据库中提取有关用户的信息,进行比较等。如果不允许用户,请返回

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CustomRequirement requirement)

如果允许用户,则使用

return Task.CompletedTask;

返回之前。然后在您的startup.cs中:

context.Succeed(requirement);

然后在您的控制器中可以添加属性

services
            .AddAuthorization(options =>
            {
                options.AddPolicy("CustomPolicy", policy =>
                    policy.Requirements.Add(new CustomRequirement()));
            })

如果不满足要求,用户将获得401未经授权,这可能不是您想要的。