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;
// This method gets called by the runtime. Use this method to add services to the container. publicvoidConfigureServices(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. publicvoidConfigure(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;
// This method gets called by the runtime. Use this method to add services to the container. publicvoidConfigureServices(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. publicvoidConfigure(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。
// This method gets called by the runtime. Use this method to add services to the container. publicvoidConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();//注册ISchedulerFactory的实例。 }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Quartz;
[PersistJobDataAfterExecution]//更新JobDetail的JobDataMap的存储副本,以便下一次执行这个任务接收更新的值而不是原始存储的值 publicclassMyJob : IJob { public Task Execute(IJobExecutionContext context) { var jobData = context.JobDetail.JobDataMap; var triggerData = context.Trigger.JobDataMap; var data = context.MergedJobDataMap;
var value1 = jobData.GetInt("key1"); var value2 = jobData.GetString("key2"); var value3 = data.GetString("key2");
var dateString = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"); Random random = new Random();
string ip = Configuration["ip"];//部署到不同服务器的时候不能写成127.0.0.1或者0.0.0.0,因为这是让服务消费者调用的地址 int port = int.Parse(Configuration["port"]);//获取服务端口 var client = new ConsulClient(ConfigurationOverview); //回调获取 var result = client.Agent.ServiceRegister(new AgentServiceRegistration() { ID = "ServerNameFirst" + Guid.NewGuid(),//服务编号保证不重复 Name = "MsgServer",//服务的名称 Address = ip,//服务ip地址 Port = port,//服务端口 Check = new AgentServiceCheck //健康检查 { DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务启动多久后反注册 Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔,或者称为心跳间隔(定时检查服务是否健康) HTTP = $"http://{ip}:{port}/api/Health",//健康检查地址 Timeout = TimeSpan.FromSeconds(5)//服务的注册时间 } });