zoukankan      html  css  js  c++  java
  • 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>

    显示结果

  • 相关阅读:
    词频统计
    第二周每周例行报告
    事务管理(ACID)
    Redis
    jar包和war包的区别
    CSS实现Loading加载动画
    如何实现“返回顶部”的页面效果
    PHP页面跳转-常见方法
    局域网络调试方式
    Thinkphp 统计数据库字段总值
  • 原文地址:https://www.cnblogs.com/qinzb/p/9503378.html
Copyright © 2011-2022 走看看