c# – 没有Identity ASP.NET Core v2的Cookie中间件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 没有Identity ASP.NET Core v2的Cookie中间件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3431字,纯文字阅读大概需要5分钟。
内容图文
我试图在不使用身份的情况下进行身份验证.我发现了一些文章描述了如何在其他版本中执行它,但对于ASP.NET Core 2没有任何内容.
以下是我拼凑在一起的内容.但是当它到达SignInAsync时会抛出异常InvalidOperationException:没有配置身份验证处理程序来处理该方案: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 core v1.x(https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie)的Microsoft文档,但是在v2中对IApplicationBuilder.UseCookieAuthentication()进行了折旧,但没有找到任何解决方案.
解决方法:
看起来Auth 2.0(https://github.com/aspnet/Announcements/issues/232)有一些重大变化
设置是正确的,但我需要做两件事:
>使用HttpContext.SignInAsync()(使用Microsoft.AspNetCore.Authentication)代替HttpContext.Authentication.SignInAsync()
>使用“AuthenticationTypes.Federation”作为身份验证类型
(注意:其他值似乎不起作用,空白将导致用户名被设置并且IsAuthenticated为false)
var identity = new ClaimsIdentity(claim,“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();
}
内容总结
以上是互联网集市为您收集整理的c# – 没有Identity ASP.NET Core v2的Cookie中间件全部内容,希望文章能够帮你解决c# – 没有Identity ASP.NET Core v2的Cookie中间件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。