这就犹如你办理了银行卡
标签:
一.Cookie是什么?我的伴侣问我cookie是什么,用来干什么的,可是我居然无法清楚大白简短地向其论述cookie,这不禁让我陷入了覃思:为什么我无法解释清楚,我对学习的要领孕育产生了怀疑!所以我们在学习一个对象的时候,必然要做到知其然知其所以然。
HTTP协议自己是无状态的。什么是无状态呢,即处事器无法判断用户身份。Cookie实际上是一小段的文本信息)。客户端向处事器倡议请求,如果处事器需要记录该用户状态,就使用response向客户端浏览器公布一个Cookie。客户端浏览器会把Cookie生存起来。当浏览器再请求该网站时,浏览器把请求的网址连同该Cookie一同提交给处事器。处事器查抄该Cookie,以此来辨认用户状态。
打个比喻,这就犹如你管理了银行卡,下次你去银行办业务,直接拿银行卡就行,不需要身份证。
二.在.NET Core中测验考试空话不久不多说,干就完了,此刻我们创建ASP.NET Core MVC项目,撰写该文章时使用的.NET Core SDK 3.0 构建的项目,创建完毕之后我们无需安置任何包,
但是我们需要在Startup中添加一些配置,用于Cookie相关的。
//public const string CookieScheme = "YourSchemeName"; 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) { //CookieAuthenticationDefaults.AuthenticationScheme Cookies Default Value //you can change scheme services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/LoginOrSignOut/Index/"; }); services.AddControllersWithViews(); // is able to also use other services. //services.AddSingleton<IConfigureOptions<CookieAuthenticationOptions>, ConfigureMyCookie>(); }
在此中我们配置登录页面,此中 AddAuthentication 中是我们的方案名称,这个是做什么的呢?很多小伙伴都懵懵懂懂暗示很懵逼啊,我看很多人也是都写得默认,那它到底有啥用,颠末我看AspNetCore源码发明它这个是可以做一些配置的。看下面的代码:
internal class ConfigureMyCookie : IConfigureNamedOptions<CookieAuthenticationOptions> { // You can inject services here public ConfigureMyCookie() {} public void Configure(string name, CookieAuthenticationOptions options) { // Only configure the schemes you want //if (name == Startup.CookieScheme) //{ // options.LoginPath = "/someotherpath"; //} } public void Configure(CookieAuthenticationOptions options) => Configure(Options.DefaultName, options); }
在此中你可以界说某些计谋,随后你直接转变 CookieScheme 的变量就可以替换某些配置,,在配置中一共有这几项,这无疑是辅佐我们快速使用Cookie的好帮忙~点个赞。
在源码中可以看到Cookie默认生存的时间是14天,这个时间我们可以去选择,撑持TimeSpan的那些类型。
public CookieAuthenticationOptions() { ExpireTimeSpan = TimeSpan.FromDays(14); ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter; SlidingExpiration = true; Events = new CookieAuthenticationEvents(); }
接下来LoginOrOut Controller,我们模拟了登录和退出,通过 SignInAsync 和 SignOutAsync 要领。
[HttpPost] public async Task<IActionResult> Login(LoginModel loginModel) { if (loginModel.Username == "haozi zhang" && loginModel.Password == "123456") { var claims = new List<Claim> { new Claim(ClaimTypes.Name, loginModel.Username) }; ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(claims, "login")); await HttpContext.SignInAsync(principal); //Just redirect to our index after logging in. return Redirect("/Home/Index"); } return View("Index"); } /// <summary> /// this action for web lagout /// </summary> [HttpGet] public IActionResult Logout() { Task.Run(async () => { //注销登录的用户,相当于ASP.NET中的FormsAuthentication.SignOut await HttpContext.SignOutAsync(); }).Wait(); return View(); }
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/33064.html
- 上一篇:发现一个很有意思的东西
- 下一篇:应该要配一个专业的技术保护人员