1-新建.net core2.1 mvc网站
2-在Startup.config文件增加相关代码, 下面代码已经配置好oidc客户端了,并设置本mvc启动ip为5009
public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddAuthentication(options => { options.DefaultScheme = "Cookies"; options.DefaultChallengeScheme = "oidc"; }) .AddCookie("Cookies") .AddOpenIdConnect("oidc", options => { options.SignInScheme = "Cookies"; options.Authority = "http://localhost:5000"; //授权服务器IP地址 options.RequireHttpsMetadata = false; options.ClientId = "mvc"; options.ClientSecret = "secret"; options.SaveTokens = true; }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
3-新建一个测试页,新加HomeController.cs
namespace MvcClient.Controllers{ [Authorize] public class HomeController : Controller { // GET: // public IActionResult Index() { return View(); } }}
home.cshtml页代码
@{ Layout = null;}Home index @foreach(var claim in User.Claims) {@claim.Type : @claim.Value}
显示结果