Esempio n. 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthentication();

            // 授权,与上一章Cookie认证中的实现一样
            app.UseAuthorize();

            // 我的信息
            app.Map("/profile", builder => builder.Run(async context =>
            {
                await context.Response.WriteHtmlAsync(async res =>
                {
                    await res.WriteAsync($"<h1>你好,当前登录用户: {HttpResponseExtensions.HtmlEncode(context.User.Identity.Name)}</h1>");
                    await res.WriteAsync("<a class=\"btn btn-default\" href=\"/Account/Logout\">退出</a>");

                    await res.WriteAsync($"<h2>AuthenticationType:{context.User.Identity.AuthenticationType}</h2>");

                    await res.WriteAsync("<h2>Claims:</h2>");
                    await res.WriteTableHeader(new string[] { "Claim Type", "Value" }, context.User.Claims.Select(c => new string[] { c.Type, c.Value }));

                    // 在第一章中介绍过HandleAuthenticateOnceAsync方法,在此调用并不会有多余的性能损耗。
                    var result = await context.AuthenticateAsync();
                    await res.WriteAsync("<h2>Tokens:</h2>");
                    await res.WriteTableHeader(new string[] { "Token Type", "Value" }, result.Properties.GetTokens().Select(token => new string[] { token.Name, token.Value }));
                });
            }));

            // 退出
            app.Map("/Account/Logout", builder => builder.Run(async context =>
            {
                await context.SignOutAsync();
                context.Response.Redirect("/");
            }));

            // 首页
            app.Run(async context =>
            {
                await context.Response.WriteHtmlAsync(async res =>
                {
                    await res.WriteAsync($"<h2>Hello OAuth Authentication</h2>");
                    await res.WriteAsync("<a class=\"btn btn-default\" href=\"/profile\">我的信息</a>");
                });
            });
        }
Esempio n. 2
0
 public async Task Profile()
 {
     await HttpContext.Response.WriteHtmlAsync(async res =>
     {
         await HttpContext.Response.WriteAsync($"<h1>你好,当前登录用户: {HttpResponseExtensions.HtmlEncode(HttpContext.User.Identity.Name)}</h1>");
         await HttpContext.Response.WriteAsync("<a class=\"btn btn-default\" href=\"/Account/Loginout\">退出</a>");
         await HttpContext.Response.WriteAsync($"<h2>AuthenticationType:{HttpContext.User.Identity.AuthenticationType}</h2>");
         await HttpContext.Response.WriteAsync("<h2>Claims:</h2>");
         await HttpContext.Response.WriteTableHeader(new string[] { "Claim Type", "Value" }, HttpContext.User.Claims.Select(c => new string[] { c.Type, c.Value }));
         var result = await HttpContext.AuthenticateAsync();
         await res.WriteAsync("<h2>Tokens:</h2>");
         await res.WriteTableHeader(new string[] { "Token Type", "Value" }, result.Properties.GetTokens().Select(token => new string[] { token.Name, token.Value }));
     });
 }
Esempio n. 3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorize();

            app.Map("/profile", builder => builder.Run(async context =>
            {
                var token = await context.GetTokenAsync("Cookie");

                await context.Response.WriteHtmlAsync(async res =>
                {
                    await res.WriteAsync($"<h1>你好,当前登录用户: {HttpResponseExtensions.HtmlEncode(context.User.Identity.Name)}</h1>");
                    await res.WriteAsync("<a class=\"btn btn-default\" href=\"/Account/Logout\">退出</a>");
                    await res.WriteAsync($"<h2>AuthenticationType:{context.User.Identity.AuthenticationType}</h2>");

                    await res.WriteAsync("<h2>Claims:</h2>");
                    await res.WriteTableHeader(new string[] { "Claim Type", "Value" },
                                               context.User.Claims.Select(c => new string[] { c.Type, c.Value }));
                });
            }));

            // 本地退出
            app.Map("/signout", builder => builder.Run(async context =>
            {
                await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                await context.Response.WriteHtmlAsync(async res =>
                {
                    await res.WriteAsync($"<h1>Signed out {HttpResponseExtensions.HtmlEncode(context.User.Identity.Name)}</h1>");
                    await res.WriteAsync("<a class=\"btn btn-default\" href=\"/\">Home</a>");
                });
            }));

            // 远程退出
            app.Map("/signout-remote", builder => builder.Run(async context =>
            {
                await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties()
                {
                    RedirectUri = "/signout"
                });
            }));
        }
Esempio n. 4
0
 public async Task CookiesLoginGet()
 {
     await HttpContext.Response.WriteHtmlAsync(async res =>
     {
         await res.WriteAsync($"<form method=\"post\">");
         await res.WriteAsync($"<input type=\"hidden\" name=\"returnUrl\" value=\"{HttpResponseExtensions.HtmlEncode(HttpContext.Request.Query["ReturnUrl"])}\"/>");
         await res.WriteAsync($"<div class=\"form-group\"><label>用户名:<input type=\"text\" name=\"userName\" class=\"form-control\"></label></div>");
         await res.WriteAsync($"<div class=\"form-group\"><label>密码:<input type=\"password\" name=\"password\" class=\"form-control\"></label></div>");
         await res.WriteAsync($"<button type=\"submit\" class=\"btn btn-default\">登录</button>");
         await res.WriteAsync($"</form>");
     });
 }