[Route("identity")] [Authorize()] public class IdentityController : ControllerBase { [HttpGet()] public IActionResult Get() { return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); } }
将 IdentityServer4.AccessTokenValidation NuGet 程序包添加到你的 API 项目
var client = new HttpClient(); var tokenClient = new TokenClient(client, new TokenClientOptions { Address = "http://localhost:5000/connect/token", ClientId = "client", ClientSecret = "secret" }); var response = await tokenClient.RequestClientCredentialsTokenAsync("api1");
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using IdentityServer4.Quickstart.UI; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting;
namespace IdentityServer4DotNet.ClientAuth.Ids4s { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews();
// or in-memory, json config //builder.AddInMemoryIdentityResources(Configuration.GetSection("IdentityResources")); //builder.AddInMemoryApiResources(Configuration.GetSection("ApiResources")); //builder.AddInMemoryClients(Configuration.GetSection("clients"));
// not recommended for production - you need to store your key material somewhere secure builder.AddDeveloperSigningCredential();
}
// 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(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles();
using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Threading.Tasks; using IdentityModel; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting;
namespace IdentityServer4DotNet.ClientAuth.MVCClient { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; });
// 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(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles();
// register your IdentityServer with Google at https://console.developers.google.com // enable the Google+ API // set the redirect URI to http://localhost:5000/signin-google options.ClientId = "629456249245-4vf1gvlltllq592coo46hrc8ofqa8cg6.apps.googleusercontent.com"; options.ClientSecret = "XhUa6NODSIGOtSPBuGrTeeeW"; });
注意:在 ASP.NET Core Identity 中使用外部认证的时候,SignInScheme 必须设置为 Identity.External,而不是 IdentityServerConstants.ExternalCookieAuthenticationScheme。