在ASP.NET Core 2.0中使用CookieAuthentication
生活随笔
收集整理的這篇文章主要介紹了
在ASP.NET Core 2.0中使用CookieAuthentication
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在ASP.NET Core中關于Security有兩個容易混淆的概念一個是Authentication(認證),一個是Authorization(授權)。而前者是確定用戶是誰的過程,后者是圍繞著他們允許做什么,今天的主題就是關于在ASP.NET Core 2.0中如何使用CookieAuthentication認證。
在ASP.NET Core 2.0中使用CookieAuthentication跟在1.0中有些不同,需要在ConfigureServices和Configure中分別設置,前者我們叫注冊服務,后者我們叫注冊中間件
? ?services.AddCookieAuthentication();
? ?services.AddMvc(options =>
? ?{
? ? ? ?var policy = new AuthorizationPolicyBuilder()
? ? ? ? ? ?.RequireAuthenticatedUser()
? ? ? ? ? ?.Build();
// 因為是后臺系統,必須登陸以后才能操作
? ? ? ?options.Filters.Add(new AuthorizeFilter(policy));
? ?});
}
{
? ?if (env.IsDevelopment())
? ?{
? ? ? ?app.UseDeveloperExceptionPage();
? ?}
? ?else
? ?{
? ? ? ?app.UseExceptionHandler("/Home/Error");
? ?}
? ?app.UseStaticFiles();
// 使用Authentication中間件
? ?app.UseAuthentication();
? ?app.UseMvc(routes =>
? ?{
? ? ? ?routes.MapRoute(
? ? ? ? ? ?name: "default",
? ? ? ? ? ?template: "{controller=Home}/{action=Index}/{id?}");
? ?});
}
在上面的services.AddCookieAuthentication中沒有任何參數,系統會為某些屬性指定默認值
public static class CookieAuthenticationDefaults
{
? ?/// <summary>
? ?/// The default value used for CookieAuthenticationOptions.AuthenticationScheme
? ?/// </summary>
? ?public const string AuthenticationScheme = "Cookies";
? ?/// <summary>
? ?/// The prefix used to provide a default CookieAuthenticationOptions.CookieName
? ?/// </summary>
? ?public static readonly string CookiePrefix = ".AspNetCore.";
? ?/// <summary>
? ?/// The default value used by CookieAuthenticationMiddleware for the
? ?/// CookieAuthenticationOptions.LoginPath
? ?/// </summary>
? ?public static readonly PathString LoginPath = new PathString("/Account/Login");
? ?/// <summary>
? ?/// The default value used by CookieAuthenticationMiddleware for the
? ?/// CookieAuthenticationOptions.LogoutPath
? ?/// </summary>
? ?public static readonly PathString LogoutPath = new PathString("/Account/Logout");
? ?/// <summary>
? ?/// The default value used by CookieAuthenticationMiddleware for the
? ?/// CookieAuthenticationOptions.AccessDeniedPath
? ?/// </summary>
? ?public static readonly PathString AccessDeniedPath = new PathString("/Account/AccessDenied");
? ?/// <summary>
? ?/// The default value of the CookieAuthenticationOptions.ReturnUrlParameter
? ?/// </summary>
? ?public static readonly string ReturnUrlParameter = "ReturnUrl";
}
根據微軟的命名規范在ConfigureServices統一使用Add***,在Configure統一使用Use***
登陸代碼
{
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(180))
});
return Redirect("/");
}
登出代碼
{
? ?await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
? ?return Redirect("/");
}
原文地址:http://www.cnblogs.com/bidianqing/p/6870163.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的在ASP.NET Core 2.0中使用CookieAuthentication的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在ASP.NET Core 2.0中使用
- 下一篇: .NET 传奇 1.0 的出版过程,以及