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. maven笔记-将本地jar包打包进可执行jar中

    参考资料:http://www.cnblogs.com/richard-jing/archive/2013/01/27/Maven_localjar.html 使用本地jar <dependen ...

  2. 设计模式(5)原型模式(Prototype)

    设计模式(0)简单工厂模式 设计模式(1)单例模式(Singleton) 设计模式(2)工厂方法模式(Factory Method) 设计模式(3)抽象工厂模式(Abstract Factory) 设 ...

  3. ZOJ - 2423-Fractal

    A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on ...

  4. 深入理解Java中的反射机制

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...

  5. go项目

    1.循环使用缓存 每条日志需要开辟缓存块来存储内容,以减少频繁的内存分配与回收.日志结构体定义如下: type MLogger struct { // freeList is a list of by ...

  6. mysql 库 行 列的 操作使用

    -----------------------------------------------------------------------------时间不等你,下一刻,全是新的.每一刻都让它变得 ...

  7. 福州大学软件工程1816 | W班 第10次作业[个人作业——软件产品案例分析]

    作业链接 个人作业--软件产品案例分析 评分细则 本次个人项目分数由两部分组成(课堂得分(老师/助教占比60%,学生占比40%)满分40分+博客分满分60分) 课堂得分和博客得分表 评分统计图 千帆竞 ...

  8. JSLinux

    JSLinuxhttps://bellard.org/jslinux/vm.html?url=https://bellard.org/jslinux/win2k.cfg&mem=192& ...

  9. 从零开始搭建VUE项目

    前言: 此样板面向大型,严肃的项目,并假定您对Webpack和vue-loader有些熟悉. 请务必阅读vue-loader的常见工作流配方的文档. 如果您只想尝试vue-loader或者鞭打一个快速 ...

  10. Oracle RMAN备份与还原

    RMAN在数据库服务器的帮助下实现数据库文件.控制文件.数据库文件与控制文件的映像副本.归档日志文件.数据库服务器参数文件的备份. RMAN的特点: (1) 支持增量备份:传统的exp与expdp备份 ...