The token generation as seen from PostMan
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Server
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddOperationalStore(
options => {
options.EnableTokenCleanup = true;
options.TokenCleanupInterval = 30;
}
)
.AddInMemoryApiResources(Config.GetApiResourses())
.AddInMemoryApiScopes(Config.GetApiScopes())
.AddInMemoryClients(Config.GetClients());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}
Config.cs
using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Server
{
public class Config
{
public static IEnumerable<ApiScope> GetApiScopes()
{
return new List<ApiScope>
{
new ApiScope(name: "read", displayName: "Read your data."),
new ApiScope(name: "write", displayName: "Write your data."),
new ApiScope(name: "delete", displayName: "Delete your data."),
new ApiScope(name: "apiscope", displayName: "Api Scope.")
};
}
public static IEnumerable<ApiResource> GetApiResourses()
{
return new List<ApiResource>
{
new ApiResource("myresourceapi", "My Resource API" )
//{
// Scopes = {new Scope("apiscope")}
//}
//This does not work; the GetApiScopes method works instead
};
}
public static IEnumerable<Client> GetClients()
{
return new[]
{
new Client
{
ClientId = "secret_client_id",
AllowedGrantTypes = {GrantType.ClientCredentials },
ClientSecrets = {
new Secret("secret".Sha256())
}
,
AllowedScopes = { "apiscope" }
}
};
}
}
}
No comments:
Post a Comment