Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CookieAuthentication in ASP.NET Core 2.0

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use CookieAuthentication in ASP.NET Core 2.0. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The use of CookieAuthentication in ASP.NET Core 2.0 is somewhat different from that in 1.0. it needs to be set up in ConfigureServices and Configure respectively, the former we call registration service, and the latter we call registration middleware

Public void ConfigureServices (IServiceCollection services) {

Services.AddCookieAuthentication ()

Services.AddMvc (options = >

{

Var policy = new AuthorizationPolicyBuilder ()

.RequireauthenticatedUser ()

.build ()

/ / because it is a background system, you must log in before you can operate it.

Options.Filters.Add (new AuthorizeFilter (policy))

});

} public void Configure (IApplicationBuilder app, IHostingEnvironment env)

{

If (env.IsDevelopment ())

{

App.UseDeveloperExceptionPage ()

}

Else

{

App.UseExceptionHandler ("/ Home/Error")

}

App.UseStaticFiles ()

/ / use Authentication middleware

App.UseAuthentication ()

App.UseMvc (routes = >

{

Routes.MapRoute (

Name: "default"

Template: "{controller=Home} / {action=Index} / {id?}")

});

}

There are no parameters in the above services.AddCookieAuthentication, and default values are specified for some properties

Public static class CookieAuthenticationDefaults

{

/ / /

/ The default value used for CookieAuthenticationOptions.AuthenticationScheme

/ / /

Public const string AuthenticationScheme = "Cookies"

/ / /

/ The prefix used to provide a default CookieAuthenticationOptions.CookieName

/ / /

Public static readonly string CookiePrefix = ".AspNetCore."

/ / /

/ The default value used by CookieAuthenticationMiddleware for the

/ CookieAuthenticationOptions.LoginPath

/ / /

Public static readonly PathString LoginPath = new PathString ("/ Account/Login")

/ / /

/ The default value used by CookieAuthenticationMiddleware for the

/ CookieAuthenticationOptions.LogoutPath

/ / /

Public static readonly PathString LogoutPath = new PathString ("/ Account/Logout")

/ / /

/ The default value used by CookieAuthenticationMiddleware for the

/ CookieAuthenticationOptions.AccessDeniedPath

/ / /

Public static readonly PathString AccessDeniedPath = new PathString ("/ Account/AccessDenied")

/ / /

/ The default value of the CookieAuthenticationOptions.ReturnUrlParameter

/ / /

Public static readonly string ReturnUrlParameter = "ReturnUrl"

}

Use Add***, uniformly in ConfigureServices and Use*** in Configure according to Microsoft's naming convention

Landing code

Public async Task LoginDo ()

{

Var user = new ClaimsPrincipal (new ClaimsIdentity (new [] {new Claim (ClaimTypes.Name, "bob")}, CookieAuthenticationDefaults.AuthenticationScheme))

Await HttpContext.SignInAsync (CookieAuthenticationDefaults.AuthenticationScheme, user, new AuthenticationProperties

{

IsPersistent = true

ExpiresUtc = DateTimeOffset.Now.Add (TimeSpan.FromDays)

})

Return Redirect (/)

}

Logout code

Public async Task Logout ()

{

Await HttpContext.SignOutAsync (CookieAuthenticationDefaults.AuthenticationScheme)

Return Redirect (/)

} about ASP.NET Core 2.0 how to use CookieAuthentication to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report