Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IdentityServer4 scopes in resource api

I am creating an api resource in my identityserver application:

new ApiResource
{
    Name = "socialnetwork",

    Scopes =
    {
        new Scope()
        {
            Name = "socialnetwork.read_contents",
            DisplayName = "Read"
        },
        new Scope
        {
            Name = "socialnetwork.share_content",
            DisplayName = "Write"
        }
    }
}

But how can I use this scopes in my socialnetwork api controllers.

public class SocialController : Controller
{
   [HttpGet]
   public async Task<IActionResult> GetCotntents(){

   }

   [HttpPost]       
   public async Task<IActionResult> ShareCotntents(string content){

   }
}
  • If a client has socialnetwork.read_contents scope, it can access GetCotntents() method.
  • If a client has socialnetwork.share_content scope, it can access ShareCotntents() method.

Actually does scope purpose is this? And how can I use it?

like image 397
barteloma Avatar asked Mar 29 '18 06:03

barteloma


People also ask

What is API scope in IdentityServer4?

This value is used for authentication with introspection and will be added to the audience of the outgoing access token. DisplayName. This value can be used e.g. on the consent screen. Description.

What is API resource IdentityServer4?

The two fundamental resource types in IdentityServer are: identity resources: represent claims about a user like user ID, display name, email address etc… API resources: represent functionality a client wants to access.

Is IdentityServer4 obsolete?

IdentityServer4 support will last until the end of life of . NET Core 3.1 that means till November 2022.

What are scopes in identityserver4?

Scopes can be used to restrict access to a resource based on read/write permissions. In IdentityServer4 scopes are modelled as resources, which come in two flavors: Identity and API.

What is the difference between API and identityserver4 resources?

In IdentityServer4 scopes are modelled as resources, which come in two flavors: Identity and API. An Identity resource allows you to model a scope that will return a certain set of claims, while an API resource scope allows you to model access to a protected resource/API. We won't be covering identity resource in this post.

How does the client request a resource from identityserver?

The client can then request the resource using the scope parameter (other parameters omitted): IdentityServer will then use the scope names to create a list of requested claim types, and present that to your implementation of the profile service. Designing your API surface can be a complicated task.

What is identityserver4 token server?

Let's build a simple Token Server using IdentityServer4 that authorizes internal/external client apps for accessing a certain Resource Server. You can think of it as a system that generates a simple data structure containing Authorization and/or Authentication information.


1 Answers

I believe that what you need to do is set up a policy for each scope you want to use (in your ConfigureServices)

 services.AddAuthorization(x =>
            {
                x.AddPolicy("readcontents", policy => 
                  policy.RequireClaim("scope", "socialnetwork.read_contents"));
                x.AddPolicy("shared", policy => 
                 policy.RequireClaim("scope", "socialnetwork.share_content"));
            });

Add then you can mark the Authorize attribute on your api method with the policy

   [HttpGet]
   [Authorize("readcontents")]
   public async Task<IActionResult> GetCotntents(){

   }

  [HttpPost]      
  [Authorize("shared")] 
  public async Task<IActionResult> ShareCotntents(string content){

  }
like image 56
jazza1000 Avatar answered Sep 29 '22 16:09

jazza1000