.NET Core 從1.1升級到2.0記錄(Cookie中介軟體踩坑)

來源:互聯網
上載者:User

標籤:分享   方法   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中介軟體踩坑)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.