ASP.NET Core 2.0 Cookie Authentication
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的更多相关文章
- asp.net core 2.0 cookie的使用
本文假设读者已经了解cookie的概念和作用,并且在传统的.net framework平台上使用过. cookie的使用方法和之前的相比也有所变化.之前是通过cookie的add.set.clear. ...
- net core体系-web应用程序-4net core2.0大白话带你入门-11asp.net core 2.0 cookie的使用
asp.net core 2.0 cookie的使用 本文假设读者已经了解cookie的概念和作用,并且在传统的.net framework平台上使用过. cookie的使用方法和之前的相比也有所 ...
- Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录
1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...
- ASP.NET Core 2.0 多应用实现Cookie共享
前言 .NET Core 2.0 发布之后,在Authentication中间件部分,相关API有不少改动(官方文档),本文主要讲的就是实现应用Cookie共享,对Cookie中间件使用不了解的可以去 ...
- ASP.NET Core 1.0 静态文件、路由、自定义中间件、身份验证简介
概述 ASP.NET Core 1.0是ASP.NET的一个重要的重新设计. 例如,在ASP.NET Core中,使用Middleware编写请求管道. ASP.NET Core中间件对HttpCon ...
- 在ASP.NET Core 2.0中使用CookieAuthentication
在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...
- ASP.NET CORE中使用Cookie身份认证
大家在使用ASP.NET的时候一定都用过FormsAuthentication做登录用户的身份认证,FormsAuthentication的核心就是Cookie,ASP.NET会将用户名存储在Cook ...
- Asp.net core 2.0.1 Razor 的使用学习笔记(三)
ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(二)—用户账户及cookie配置 修改用户账户及cookie配置 一.修改密码强度和用户邮箱验证规则 ...
- 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 ...
随机推荐
- mysql 索引原理
一.索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构. 我们知道,数据库查询是数据库的最主要功能之 ...
- 用包来组织模型 -- Django从入门到精通系列教程
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. 在我们使用python manage.py ...
- Fiddler抓包和修改WebSocket数据,支持wss
记录一下用Fiddler对WebSocket收发的数据进行抓包分析和篡改数据,只找到这么一个方法,能用就行吧. 时间:2019-3-29 环境: win7 + Fiddler 5.0 Fiddler抓 ...
- Python 学习 第十篇:正则表达式 - re
规则表达式(Regular Expression, RE),又称作正则表达式,通常用于检索.替换符合指定规则的文本,正则表达式定义的规则,称作模式(Pattern),即正则表达式的作用是从文本中查找到 ...
- python--__init__()方法和__new__()方法
这两个方法是python类中的基本方法,经常会在一些面试中问到.即便没有要面试之类的,学习一下其内部的原理和使用也是有必要的. 首先区分一下这两个方法: __init__:初始化方法 __new__: ...
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)
D - Various Sushi Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement ...
- 安装pandas时出现环境错误
在安装pandas时出现Could not install packages due to an EnvironmentErrorConsider using the `--user` option ...
- java总结:double取两位小数的多种方法
1.方法一 四舍五入: import java.math.BigDecimal; double f = 111231.5585; BigDecimal b = new BigDecimal(f); d ...
- react render
实际上react render方法返回一个虚拟dom 并没有去执行渲染dom 渲染的过程是交给react 去完成的 这就说明了为什么要在所有数据请求完成后才去实现render 这样做也提高了性能.只调 ...