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. [MicroPython]TPYBoardv102超全DIY案例一览

    TPYBoardv102是一款比较成熟和经典的MicroPython开发板,不仅适用于学习实验还能应用于大型的工业级开发,是MicroPython入门和研发的不二之选.下面我们就扒开v102的“层层外 ...

  2. Kafka 入门三问

    目录 1 Kafka 是什么? 1.1 背景 1.2 定位 1.3 产生的原因 1.4 Kafka 有哪些特征 消息和批次 模式 主题和分区 生产者和消费者 broker 和 集群 1.5 Kafka ...

  3. el-date-picker 快捷日期简单计算

    const oneDaySeconds = 3600 * 1000 * 24 pickerOptions: { shortcuts: [ { text: '今天', onClick(picker) { ...

  4. wtf_1234

    好无聊啊,今天困的厉害. 不想做任何事情 wtf bitch!

  5. 几何学观止(Lie群部分)

    上承这个页面,这次把Lie群的部分写完了 几何学观止-微分几何部分(20181102).pdf 我觉得其他部分(尤其是代数几何部分)我目前没有把握写得令自己满意,总之希望在毕业前能写完吧. 这次调整了 ...

  6. Python-time模块-58

    time 模块: Eva_J import time time.sleep(100) #时间睡眠 print(time.time()) #返回一个以秒为单位的时间 时间模块 和时间有关系的我们就要用到 ...

  7. H5 60-浮动元素排序规则

    60-浮动元素排序规则 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  8. Sparse Principal Component Analysis via Regularized Low Rank Matrix Approximation(Adjusted Variance)

    目录 前言 文章概述 固定\(\widetilde{\mathrm{v}}\) 固定\(\widetilde{\mathrm{u}}\) Adjusted Variance 前言 这篇文章用的也是交替 ...

  9. hadoop和java 配置环境变量的的tar

    第一步:打开工具上传tar包 如下图 第二步:在文件路径下查看是否上传成功 第三步:解压tar包               tar -zxvf hadoop.2.6.5.tar.gz 第四步:配置环 ...

  10. 敏捷开发、DevOps相关书籍——书单

    自己瞎整理的一些书单,都是豆瓣评分比较高的书,可以作为选择的一个参考. 书名 豆瓣链接 持续交付:发布可靠软件的系统方法 https://book.douban.com/subject/6862062 ...