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>

    显示结果

  • 相关阅读:
    事件
    DOM中对象的获得
    C# 字符串 相关操作
    两个listbox 复制
    C#窗体控件简介ListBox
    store procedure
    view_baseInfo
    不走弯路,就是捷径
    inherit
    Excel 版本对应
  • 原文地址:https://www.cnblogs.com/qinzb/p/9503378.html
Copyright © 2011-2022 走看看