在ASP.NET Core 2.0中,.UseAuthentication()中间件有一个重大变化,不再允许old syntax mentioned here工作。
新版本似乎在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位中再次略有改变
事实证明它比预期的要容易得多,但由于the official documentation还没有更新,这正是适用于纯Cookie认证的:
组态:
在ConfigureServices()
中配置特定的身份验证机制:
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
调用中添加授权,你只需在这里设置标准中间件,请参阅this announcement。
旧:
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 Core Announcements GitHub Repository,其中ASP.NET核心团队宣布下一版本的重大变化,只需在那里选择一个特定的里程碑,即2.0.0-preview1,2.0.0-preview2等。