zoukankan      html  css  js  c++  java
  • ASP.NET Core Cookie-based认证实现

    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    
    namespace HelloCore2.Controllers
    {
        [Authorize]
        public class AdminController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
        }
    }
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;
    using System.Security.Claims;
    
    namespace HelloCore2.Controllers
    {
        public class AccountController : Controller
        {
            public IActionResult Login()
            {
                var claims = new List<Claim>()
                {
                    new Claim(ClaimTypes.Name,"wolf"),
                    new Claim(ClaimTypes.Role,"admin"),
                };
                var claimidentity = new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);
                HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimidentity));
                return Ok("Login");
            }
    
            public IActionResult LogOut()
            {
                HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                return Ok("LogOut");
            }
        }
    }
    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace HelloCore2
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                //配置1
                services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                    .AddCookie(options =>
                    {
                        //跳转登录页面
                        options.LoginPath = "/Account/login";
                    });
                services.AddMvc();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseBrowserLink();
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseStaticFiles();
                //配置2
                app.UseAuthentication();
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }
        }
    }

    登录成功

  • 相关阅读:
    在autolayout中加入每个view的weight
    iOS 拨打电话
    20141211笔记(UIImageView 设置内容的Mode的方法UICollectionViewCell Custom的方法ios modal segue code)
    UILabel总结(转载)
    Error:duplicate files during packaging of APK app/build/output/apk
    《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作
    《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础
    《UNIX-Shell编程24学时教程》读书笔记chap7 变量
    《软件调试的艺术》读书笔记
    ubuntu环境准备
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/9106433.html
Copyright © 2011-2022 走看看