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>

 

显示结果

 

posted @ 2018-08-20 00:12  深圳丶追  阅读(300)  评论(0编辑  收藏  举报