using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks; namespace Fiver.Security.Authentication
{
public class Startup
{
public void ConfigureServices(
IServiceCollection services)
{
services.AddAuthentication("FiverSecurityScheme")
.AddCookie("FiverSecurityScheme", options =>
{
options.AccessDeniedPath = new PathString("/Security/Access");
options.Cookie = new CookieBuilder
{
//Domain = "",
HttpOnly = true,
Name = ".Fiver.Security.Cookie",
Path = "/",
SameSite = SameSiteMode.Lax,
SecurePolicy = CookieSecurePolicy.SameAsRequest
};
options.Events = new CookieAuthenticationEvents
{
OnSignedIn = context =>
{
Console.WriteLine("{0} - {1}: {2}", DateTime.Now,
"OnSignedIn", context.Principal.Identity.Name);
return Task.CompletedTask;
},
OnSigningOut = context =>
{
Console.WriteLine("{0} - {1}: {2}", DateTime.Now,
"OnSigningOut", context.HttpContext.User.Identity.Name);
return Task.CompletedTask;
},
OnValidatePrincipal = context =>
{
Console.WriteLine("{0} - {1}: {2}", DateTime.Now,
"OnValidatePrincipal", context.Principal.Identity.Name);
return Task.CompletedTask;
}
};
//options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.LoginPath = new PathString("/Security/Login");
options.ReturnUrlParameter = "RequestPath";
options.SlidingExpiration = true;
}); services.AddMvc();
} //public void ConfigureServices(
// IServiceCollection services)
//{
// services.AddAuthentication("FiverSecurityScheme")
// .AddCookie("FiverSecurityScheme", options =>
// {
// options.AccessDeniedPath = new PathString("/Security/Access");
// options.LoginPath = new PathString("/Security/Login");
// }); // services.AddMvc();
//} public void Configure(
IApplicationBuilder app,
IHostingEnvironment env)
{
app.UseDeveloperExceptionPage(); app.UseAuthentication(); app.UseMvcWithDefaultRoute();
}
}
}
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Fiver.Security.Authentication.Models.Security;
using System.Security.Claims;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authentication;
using System; namespace Fiver.Security.Authentication.Controllers
{
public class SecurityController : Controller
{
public IActionResult Login(string requestPath)
{
ViewBag.RequestPath = requestPath ?? "/";
return View();
} [HttpPost]
public async Task<IActionResult> Login(LoginInputModel inputModel)
{
if (!IsAuthentic(inputModel.Username, inputModel.Password))
return View(); // create claims
List<Claim> claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "Sean Connery"),
new Claim(ClaimTypes.Email, inputModel.Username)
}; // create identity
ClaimsIdentity identity = new ClaimsIdentity(claims, "cookie"); // create principal
ClaimsPrincipal principal = new ClaimsPrincipal(identity); // sign-in
await HttpContext.SignInAsync(
scheme: "FiverSecurityScheme",
principal: principal,
properties: new AuthenticationProperties
{
//IsPersistent = true, // for 'remember me' feature
//ExpiresUtc = DateTime.UtcNow.AddMinutes(1)
}); return Redirect(inputModel.RequestPath ?? "/");
//return RedirectToAction("Index", "Home");
} public async Task<IActionResult> Logout(string requestPath)
{
await HttpContext.SignOutAsync(
scheme: "FiverSecurityScheme"); return RedirectToAction("Login");
} public IActionResult Access()
{
return View();
} #region " Private " private bool IsAuthentic(string username, string password)
{
return (username == "james" && password == "bond");
} #endregion
}
}

ASP.NET Core 2.0 Cookie Authentication的更多相关文章

  1. asp.net core 2.0 cookie的使用

    本文假设读者已经了解cookie的概念和作用,并且在传统的.net framework平台上使用过. cookie的使用方法和之前的相比也有所变化.之前是通过cookie的add.set.clear. ...

  2. net core体系-web应用程序-4net core2.0大白话带你入门-11asp.net core 2.0 cookie的使用

    asp.net core 2.0 cookie的使用   本文假设读者已经了解cookie的概念和作用,并且在传统的.net framework平台上使用过. cookie的使用方法和之前的相比也有所 ...

  3. Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录

    1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...

  4. ASP.NET Core 2.0 多应用实现Cookie共享

    前言 .NET Core 2.0 发布之后,在Authentication中间件部分,相关API有不少改动(官方文档),本文主要讲的就是实现应用Cookie共享,对Cookie中间件使用不了解的可以去 ...

  5. ASP.NET Core 1.0 静态文件、路由、自定义中间件、身份验证简介

    概述 ASP.NET Core 1.0是ASP.NET的一个重要的重新设计. 例如,在ASP.NET Core中,使用Middleware编写请求管道. ASP.NET Core中间件对HttpCon ...

  6. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  7. ASP.NET CORE中使用Cookie身份认证

    大家在使用ASP.NET的时候一定都用过FormsAuthentication做登录用户的身份认证,FormsAuthentication的核心就是Cookie,ASP.NET会将用户名存储在Cook ...

  8. Asp.net core 2.0.1 Razor 的使用学习笔记(三)

    ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(二)—用户账户及cookie配置 修改用户账户及cookie配置 一.修改密码强度和用户邮箱验证规则 ...

  9. Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

随机推荐

  1. P3374 【模板】树状数组 1--洛谷luogu

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  2. python:HTMLTestRunner测试报告优化

    之前的博客有介绍过python的单元测试框架unittest,基于其扩展的测试报告模块HTMLTestRunner,不过这个报告本身的界面看起来太丑... 趁着今天有时间,找了两个二次开发优化后的HT ...

  3. 使用Git进行代码管理的心得--github for windows

    首先简述一下Git进行代码管理的情况 我使用的是github for windows,官网下载的速度太慢,所以用了离线安装包.安装之后会有GitHub和GitShell两个软件,其中Github采用图 ...

  4. a2dp播放流程源码分析

    之前分析了a2dp profile 的初始化的流程,这篇文章分析一下,音频流在bluedroid中的处理流程. 上层的音频接口是调用a2dp hal 里面的接口来进行命令以及数据的发送的. 关于控制通 ...

  5. Jmeter(三十七)循环控制器+交替控制器+事务控制器 完美实现接口字段参数化校验

    我们在做接口自动化的时候,常常因为无法灵活的的校验接口字段而烦恼.不能自动校验接口字段的脚本,也就不能称之为接口自动化.因此,我设计了一套组合式的控制器,可以完美的解决这个问题 1:首先我们需要在本地 ...

  6. Python_练习题_49

    # 3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb name=['alex','wupeiqi','yuanhao','nezha'] def func(item): re ...

  7. pip3 升级失败的解决方法!亲测有效

    pip3 --default-timeout=10000 install -U pip 注意:由于防火长城的存在,会导致更新失败,如果你加上--default-timeout=10000  这个就ok ...

  8. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  9. Javascript模板引擎handlebars使用

    源地址:http://rfyiamcool.blog.51cto.com/1030776/1278620 代码示例: <!DOCTYPE html> <html> <he ...

  10. centos安装bundle文件

    centos安装VMware-Workstation-Full-*.bundle那点事 | 鳗鱼是条狗https://kinggoo.com/centos-vmware.htm Linux 下 VMW ...