ASP.NET Core 2.0预览版1:如何使用自定义登录路径设置Cookie身份验证 [英] ASP.NET Core 2.0 Preview 1: How to set up Cookie Authentication with custom login path
问题描述
在ASP.NET Core 2.0中,.UseAuthentication()中间件具有重大变化,不再允许
In ASP.NET Core 2.0 the .UseAuthentication() middleware has a breaking change that no longer allows the old syntax mentioned here to work.
新版本似乎可以处理addAuthentication中的config,但是我找不到任何有关如何更改指定自定义登录和注销网址的旧代码的详细信息.
The new version appears to deal with config in addAuthentication, but I can't find any details anywhere on how to change my old code that specified a custom login and logout 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;
});
任何帮助将不胜感激...
Any help would be appreciated...
推荐答案
已更新,因为它在2.0 RTM位中再次略有变化
事实证明,它比预期的要容易得多,但由于官方文档尚未更新,这正是纯Cookie身份验证的有效方法:
It turns out it's a lot easier than expected, but as the official documentation hasn't been updated yet, here is exactly what works for plain Cookie auth:
配置:
在ConfigureServices()
中配置特定的身份验证机制:
In ConfigureServices()
configure the specific Authentication mechanism:
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o =>
{
o.LoginPath = "/api/login";
o.LogoutPath = "/api/logout";
// additional config options here
});
然后在Configure()
中实际挂接中间件:
Then in Configure()
to actually hook up the middleware:
app.UseAuthentication();
使用身份验证组件
然后使用逻辑已从HttpContext.Authentication
对象转移到应用程序逻辑(例如控制器代码)中的HttpContext
的实际Auth组件:
Then to use the actual Auth components the logic has shifted from the HttpContext.Authentication
object, down to just HttpContext
in application logic like controller code:
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity));
或:
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
这篇关于ASP.NET Core 2.0预览版1:如何使用自定义登录路径设置Cookie身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!