16-oauth2-oidc-Client实现
Posted qinzb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了16-oauth2-oidc-Client实现相关的知识,希望对你有一定的参考价值。
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: /<controller>/ public IActionResult Index() { return View(); } } }
home.cshtml页代码
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Home index</title> </head> <body> @foreach(var claim in User.Claims) { <div>@claim.Type : @claim.Value</div> } </body> </html>
显示结果
以上是关于16-oauth2-oidc-Client实现的主要内容,如果未能解决你的问题,请参考以下文章