Search code examples
authenticationasp.net-coreasp.net-web-api2asp.net-identityidentityserver4

How Use Authentication and Authoriziation of my IdentityServer with itself?


I have an Api with Name MyApi and I use another asp.net core application with Identityserver4 for Protect MyApi,Now I don't have any problem in MyApi but,I want to save my Users's NationalCode ,So I should save this in my IdentityServer Database,But can't Get UserId (with User.Identity.Name) in my IdentityServer Project,I had same problem in my previose question

User.Identity.Name is null in my ASP.NET Core Web API

Now I have this problem in my IdentityServer4 project,So

Can I use Of MyApi token Or I should get a new token for send request to my idenittyserver4 project?

If I can MyAPI token ,How should I add configuration to solve the problem?

If I should take new token for my IdentityServer4 project,DO I need to want users to login again?!!!

Edit

I found a tutorail in below link but My Problem not solved Yet.

http://docs.identityserver.io/en/latest/topics/add_apis.html

I have seed my IdentityDatabase with below method


public async Task AddIdenityServerApiToResources(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
            {
                serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
                var ccontext = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
                ccontext.Database.Migrate();
                //=============================================================
                ccontext.ApiResources.Add(new ApiResource(IdentityServerConstants.LocalApi.ScopeName) {
                    UserClaims =
                    {
                        JwtClaimTypes.Name,
                        JwtClaimTypes.Subject,
                        JwtClaimTypes.Role,
                    }
                }.ToEntity());
                //Add ApiResource To Client's Scope
                var Clients = ccontext.Clients.Include(e => e.AllowedScopes);
                foreach (var item in Clients)
                {
                    item.AllowedScopes.Add(new IdentityServer4.EntityFramework.Entities.ClientScope() { Scope = IdentityServerConstants.LocalApi.ScopeName });
                }
                var Count = await ccontext.SaveChangesAsync();
                if (Count > 0)
                {
                }
            }
        }

Solution

  • I solved the problem with below link:

    http://docs.identityserver.io/en/latest/topics/add_apis.html

    But the problem was where that I don't used Authorize on my controller with LocalApi.PolicyName policy

    [Route("localApi")]
    [Authorize(LocalApi.PolicyName)]
    public class LocalApiController : ControllerBase
    {
        public IActionResult Get()
        {
            // omitted
        }
    }
    

    after that the prolem was solvled