Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the list of policies from Asp.Net Core Authentication?

I am creating a web application using Asp.Net Core 1.1.1 and its Authorization/Authentication system.

In my startup.cs file I have configured the various policies I need:

public void ConfigureServices(IServiceCollection services)
{
        servizi.AddAuthorization(options =>
        {
            options.AddPolicy("Insert", policyBuilder => policyBuilder .RequireClaim("AllowInsert"));
            options.AddPolicy("Update", policyBuilder => policyBuilder .RequireClaim("AllowUpdate"));
            options.AddPolicy("Delete", policyBuilder => policyBuilder .RequireClaim("AllowDelete"));
        });
}

Then in a controller I have assigned the claims to a role (the Administrator role):

    public async Task<IActionResult> AssignClaimstoAdminRole()
    {
        await _roleManager.AddClaimAsync(await _roleManager.FindByNameAsync("Administrator"), new Claim("AllowInsert", "true"));
        await _roleManager.AddClaimAsync(await _roleManager.FindByNameAsync("Administrator"), new Claim("AllowUpdate", "true"));
        await _roleManager.AddClaimAsync(await _roleManager.FindByNameAsync("Administrator"), new Claim("AllowDelete", "true"));

        await _loginManager.RefreshSignInAsync(await _userManager.FindByNameAsync(User.Identity.Name));
        return RedirectToAction("SomeAction", "MyController");
    }

and finally I have protected my views injecting the Authorization Service:

@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService

@if (await AuthorizationService.AuthorizeAsync(User, "Insert"))
{
     <i role="submit" class="fa fa-floppy-o fa-2x"></i>
}

My question is: I would like to retrieve the list of the policies created in startup.cs, so that eventually I can build a page to add other users/roles assigning them one or more policies already present in the system.

Is there an object which exposes the collection of previously added policies? Thanks in advance!

like image 270
Giorgio Borgo Avatar asked Mar 15 '17 13:03

Giorgio Borgo


People also ask

What is policy-based authorization in .NET core?

In ASP.NET Core, the policy-based authorization framework is designed to decouple authorization and application logic. Simply put, a policy is an entity devised as a collection of requirements, which themselves are conditions that the current user must meet.

What does HttpContext SignInAsync do?

SignInAsync(HttpContext, String, ClaimsPrincipal, AuthenticationProperties) Sign in a principal for the specified scheme.

How many types of authentication are there in ASP.NET Core?

ASP.NET supports Forms Authentication, Passport Authentication, and Windows authentication providers. The mode is set to one of the authentication modes: Windows, Forms, Passport, or None. The default is Windows.

What is CookieAuthenticationDefaults AuthenticationScheme?

The CookieAuthenticationDefaults. AuthenticationScheme GitHub Source shows it's set to "Cookies" . The authentication cookie's IsEssential property is set to true by default. Authentication cookies are allowed when a site visitor hasn't consented to data collection.


1 Answers

Better rely on claims. You always has access to the list of claims.

I'm currently working on this, where application's controllers use policies to grant access, and my menu builder uses claims to add or skip menu item.

I've thought that I can use the same string for claim value and policy name, thus easier to manage.

I've found an interesting article too, about complex filtering, policies and claims: https://andrewlock.net/custom-authorisation-policies-and-requirements-in-asp-net-core/

like image 106
barbara.post Avatar answered Sep 24 '22 17:09

barbara.post