分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

ASP.NET Core 2.0 Cookie-based认证实现

发布时间:2023-09-06 01:57责任编辑:沈小雨关键词:.NETCookie
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?}"); ???????????}); ???????} ???}}

登录成功

ASP.NET Core 2.0 Cookie-based认证实现

原文地址:https://www.cnblogs.com/lgxlsm/p/9106433.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved