ASP.NET Core 2.0 Preview 1:如何使用自定义登录路径设置Cookie身份validation
在ASP.NET Core 2.0中,.UseAuthentication()中间件有一个重大更改,不再允许此处提到的旧语法工作。
新版本似乎在addAuthentication中处理配置,但我无法在任何地方找到有关如何更改指定自定义登录和注销URL的旧代码的任何详细信息。
services.AddAuthentication(o => { // Where can I specify this????? var opt = new CookieAuthenticationOptions() { LoginPath = "/api/login", LogoutPath = "/api/logout", }; o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; });
任何帮助,将不胜感激…
更新后,在2.0 RTM位中再次略有改变
事实certificate它比预期的要容易得多,但由于官方文档还没有更新,这正是适用于纯Cookie认证的:
组态:
在ConfigureServices()
配置特定的身份validation机制:
services .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(o => { o.LoginPath = "/api/login"; o.LogoutPath = "/api/logout"; // additional config options here });
然后在Configure()
中实际连接中间件:
app.UseAuthentication();
使用Auth组件
然后使用实际的Auth组件,逻辑已从HttpContext.Authentication
对象转移到应用程序逻辑中的HttpContext
,如控制器代码:
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
要么:
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
您发布的示例似乎不是真正的代码(即new CookieAuthenticationOptions()
在AddAuthentication调用中,而不是作为AddCookieAuthentication
参数)。 您没有在AddAuthorization
调用中添加授权,您只需在此设置标准中间件,请参阅此公告 。
旧:
services.AddAuthentication(sharedOptions => sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions AutomaticChallenge = true, AutomaticAuthenticate = true,
新:
app.AddAuthentication(o => { o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; });
而且
services.AddXxxAuthentication(new XxxOptions() { ... });
被替换为
services.AddXxxAuthentication(options => { });
与所有其他接受配置的方法一致。
另外值得一看的是ASP.NET核心公告GitHub存储库 ,其中ASP.NET核心团队宣布下一版本的重大更改,只需在那里选择一个特定的里程碑,即2.0.0-preview1,2.0.0 -preview2 ,等等