没有身份的 Cookie 中间件 ASP.NET Core v2
Cookie Middleware without Identity ASP.NET Core v2
我想在不使用身份的情况下进行身份验证。我找到了一些描述如何在其他版本中执行此操作的文章,但是 ASP.NET Core 2 没有任何内容。
下面是我拼凑的。但是当它到达 SignInAsync 时抛出异常 InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
{
o.LoginPath = new PathString("/Account/Login/");
o.AccessDeniedPath = new PathString("/Account/Forbidden/");
});
services.AddAuthentication();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseAuthentication();
}
public async Task<IActionResult> Login()
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "joe nobody")
};
var identity = new ClaimsIdentity(claims, "MyCookieMiddlewareInstance");
var principal = new ClaimsPrincipal(identity);
//blows up on the following statement:
//InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);
return View();
}
asp.net 核心 v1.x (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie) 有一份 Microsoft 文档,但是 IApplicationBuilder.UseCookieAuthentication() 在 v2 中已贬值并且尚未找到任何解决方案。
Auth 2.0 似乎有一些重大变化 (https://github.com/aspnet/Announcements/issues/232)
设置正确,但我需要做两件事:
- 使用
HttpContext.SignInAsync()
(using Microsoft.AspNetCore.Authentication
) 而不是 HttpContext.Authentication.SignInAsync()
- 使用
"AuthenticationTypes.Federation"
作为身份验证类型
(注意:其他值似乎不起作用,空白将导致用户名被设置并且 IsAuthenticated 为 false)
var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");
下面是更正后的代码
在Startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
{
o.LoginPath = new PathString("/Account/Login/");
o.AccessDeniedPath = new PathString("/Account/Forbidden/");
});
services.AddAuthentication();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
在控制器中
using Microsoft.AspNetCore.Authentication;
//...
public async Task<IActionResult> Login()
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "joe nobody")
};
var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync("MyCookieMiddlewareInstance", principal);
return View();
}
我想在不使用身份的情况下进行身份验证。我找到了一些描述如何在其他版本中执行此操作的文章,但是 ASP.NET Core 2 没有任何内容。
下面是我拼凑的。但是当它到达 SignInAsync 时抛出异常 InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
{
o.LoginPath = new PathString("/Account/Login/");
o.AccessDeniedPath = new PathString("/Account/Forbidden/");
});
services.AddAuthentication();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseAuthentication();
}
public async Task<IActionResult> Login()
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "joe nobody")
};
var identity = new ClaimsIdentity(claims, "MyCookieMiddlewareInstance");
var principal = new ClaimsPrincipal(identity);
//blows up on the following statement:
//InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);
return View();
}
asp.net 核心 v1.x (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie) 有一份 Microsoft 文档,但是 IApplicationBuilder.UseCookieAuthentication() 在 v2 中已贬值并且尚未找到任何解决方案。
Auth 2.0 似乎有一些重大变化 (https://github.com/aspnet/Announcements/issues/232)
设置正确,但我需要做两件事:
- 使用
HttpContext.SignInAsync()
(using Microsoft.AspNetCore.Authentication
) 而不是HttpContext.Authentication.SignInAsync()
- 使用
"AuthenticationTypes.Federation"
作为身份验证类型 (注意:其他值似乎不起作用,空白将导致用户名被设置并且 IsAuthenticated 为 false)var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");
下面是更正后的代码
在Startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
{
o.LoginPath = new PathString("/Account/Login/");
o.AccessDeniedPath = new PathString("/Account/Forbidden/");
});
services.AddAuthentication();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
在控制器中
using Microsoft.AspNetCore.Authentication;
//...
public async Task<IActionResult> Login()
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "joe nobody")
};
var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync("MyCookieMiddlewareInstance", principal);
return View();
}