標籤:分享 方法 text on() logout 提前 nas 異常 登入
.NET Core 2.0 新時代
萬眾矚目的.NET Core 2.0終於發布了,原定於9.19的dotnetconf大會的發布時間大大提前了1個月,.NET Core 2.0/.NET Standard 2.0的正式發布是.NET 開源跨平台的一個重大裡程碑。
.NET Core 2.0 SDK:https://www.microsoft.com/net/download/core#/sdk
Visual Studio 2017 15.3:https://www.visualstudio.com/zh-hans/downloads/
更新ASP.NET Core 項目中的目標框架
更新ASP.NET Core Web項目中的Nuget依賴項
備忘:刪除之前的 Microsoft.* 依賴項,使用 Microsoft.AspNetCore.All。
.NET Core 2.0 更新Cookie中介軟體使用方法
備忘:我這邊當前項目單獨使用的Cookie中介軟體,未結合Identity使用。
1:在ConfigureServices添加Cookie中介軟體,使用自訂Scheme(坑就在這裡)
services.AddAuthentication(options=> { options.DefaultChallengeScheme = CookieAuthenInfo.QwCmsWebCookieInstance; options.DefaultSignInScheme = CookieAuthenInfo.QwCmsWebCookieInstance; options.DefaultAuthenticateScheme = CookieAuthenInfo.QwCmsWebCookieInstance; }) .AddCookie(CookieAuthenInfo.QwCmsWebCookieInstance, m => { m.LoginPath = new PathString("/Account/Login"); m.AccessDeniedPath = new PathString("/Account/Denied"); m.LogoutPath = new PathString("/Account/Logout"); m.Cookie.Path = "/"; });
踩坑:報異常 No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
特別感謝Zonciu提供的協助。
2:在Configure使用Cookie中介軟體
app.UseAuthentication();
使用Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions新的擴充方法
使用方式
//登入await HttpContext.SignInAsync(CookieAuthenInfo.QwCmsWebCookieInstance, userPrincipal, new AuthenticationProperties { ExpiresUtc = DateTime.UtcNow.AddHours(12), IsPersistent = true, AllowRefresh = false });//退出await HttpContext.SignOutAsync(CookieAuthenInfo.QwCmsWebCookieInstance);
AuthorizeAsync現在返回結果為AuthorizationResult新的擴充方法
使用方式
var result = await HttpContext.AuthenticateAsync("xxxx");if (result.Succeeded){ ......}
參考
1:[Draft] Auth 2.0 Migration announcement #1310
2:Migrating Authentication and Identity to ASP.NET Core 2.0
.NET Core 從1.1升級到2.0記錄(Cookie中介軟體踩坑)