Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have developed Identity server in asp.net core the problem is I want to use dhis2 users credentials to authenticate the user and get the token that can be used in authentication of the API's below is the implementation


What I have tried:

Identity Server config class

<pre>public class Config
 {

     public static IEnumerable<ApiResource> GetApiResources()
     {
         return new List<ApiResource>
         {
             new ApiResource("myresourceapi", "My Resource API")
             {
                 Scopes =
                 {
                     new Scope("openid")
                 }
             }
         };
     }

     public static IEnumerable<Client> GetClients()
     {
         return new[]
         {
            
             new Client
             {
             ClientId = "application-key-2024",
             AllowedGrantTypes = GrantTypes.ClientCredentials,
             ClientSecrets = 
             { 
                     new Secret("001b8a318-71dc-6700-982e-dd592d0f131".Sha256()) 
             },
             
             RedirectUris = { "http://www.example.org" },
             AllowedScopes = { "openid"},
             RequireConsent = false
             }
         };
     }

 }




Program.cs class

using IdentityServer;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddIdentityServer()
             .AddDeveloperSigningCredential()
             .AddOperationalStore(options =>
             {
                 options.EnableTokenCleanup = true;
                 options.TokenCleanupInterval = 30; // interval in seconds
             })
             .AddInMemoryApiResources(Config.GetApiResources())
             .AddInMemoryClients(Config.GetClients());

var app = builder.Build();

app.UseIdentityServer();

app.Run();



my service Program.cs class that consumes the token generated


var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(o =>
        {
            o.Authority = "https://localhost:7197";
            o.Audience = "myresourceapi";
            o.RequireHttpsMetadata = false;
        });

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("PublicSecure", policy => policy.RequireClaim("application-key-2024", "001b8a318-71dc-6700-982e-dd592d0f131"));
});



ar app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();



Controller class in microservice class


namespace Data_Uploading_Service.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UploadController : ControllerBase
    {

    [HttpGet]
    [Authorize(Policy = "PublicSecure")]
    public String DataApi()
    {
    return "You have been authenticated";
    }
    
    }
}


So now in order to be authenticated from post man you should provide client ID, client secret, scope and grant_type but what I want is to provide username and password e.g uname = admin, password = district
Posted

1 solution

So you are looking at setting up password flow for this. You can find details on how to accomplish this from here[^].
 
Share this answer
 
Comments
Office Systems 3-May-24 2:01am    
Thanks @pete O'Hanlon I already have this link, my question is how can I call the DHIS2 endpoints (/api/25/me/authorities) from my Identity server in .net core so that when I enter username and password from postman it get authenticated from the end point then Identity server will have to generate the token for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900