如何在ASP.NET Core 2.0中设置多个身份验证方案?
|
我正在尝试将我的auth内容迁移到Core 2.0并使用我自己的身份验证方案出现问题.我在启动时的服务设置如下所示: var authenticationBuilder = services.AddAuthentication(options =>
{
options.AddScheme("myauth",builder =>
{
builder.HandlerType = typeof(CookieAuthenticationHandler);
});
})
.AddCookie();
我在控制器中的登录代码如下所示: var claims = new List<Claim>
{
new Claim(ClaimTypes.Name,user.Name)
};
var props = new AuthenticationProperties
{
IsPersistent = persistCookie,ExpiresUtc = DateTime.UtcNow.AddYears(1)
};
var id = new ClaimsIdentity(claims);
await HttpContext.SignInAsync("myauth",new ClaimsPrincipal(id),props);
但是当我在控制器或动作过滤器中时,我只有一个身份,并且它不是经过身份验证的身份: var identity = context.HttpContext.User.Identities.SingleOrDefault(x => x.AuthenticationType == "myauth"); 导航这些变化一直很困难,但我猜我正在做.AddScheme错了.有什么建议? 编辑:这里(基本上)是一个干净的应用程序,不会在User.Identies上产生两组身份: namespace WebApplication1.Controllers
{
public class Testy : Controller
{
public IActionResult Index()
{
var i = HttpContext.User.Identities;
return Content("index");
}
public async Task<IActionResult> In1()
{
var claims = new List<Claim> { new Claim(ClaimTypes.Name,"In1 name") };
var props = new AuthenticationProperties { IsPersistent = true,ExpiresUtc = DateTime.UtcNow.AddYears(1) };
var id = new ClaimsIdentity(claims);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,props);
return Content("In1");
}
public async Task<IActionResult> In2()
{
var claims = new List<Claim> { new Claim(ClaimTypes.Name,"a2 name") };
var props = new AuthenticationProperties { IsPersistent = true,ExpiresUtc = DateTime.UtcNow.AddYears(1) };
var id = new ClaimsIdentity(claims);
await HttpContext.SignInAsync("a2",props);
return Content("In2");
}
public async Task<IActionResult> Out1()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Content("Out1");
}
public async Task<IActionResult> Out2()
{
await HttpContext.SignOutAsync("a2");
return Content("Out2");
}
}
}
和初创公司: namespace WebApplication1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie("a2");
services.AddMvc();
}
public void Configure(IApplicationBuilder app,IHostingEnvironment env)
{
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
解决方法
不要使用AddScheme:它是为处理程序编写者设计的低级方法.
要注册cookie处理程序,只需执行以下操作: public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = "myauth1";
})
.AddCookie("myauth1");
.AddCookie("myauth2");
}
public void Configure(IApplicationBuilder app)
{
app.UseAuthentication();
// ...
}
}
重要的是要注意,你不能像在1.x中那样注册多个默认方案(这个巨大的重构的重点是避免同时拥有多个自动认证中间件). 如果您绝对需要在2.0中模拟此行为,则可以编写手动调用AuthenticateAsync()的自定义中间件,并创建包含所需身份的ClaimsPrincipal: public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = "myauth1";
})
.AddCookie("myauth1");
.AddCookie("myauth2");
}
public void Configure(IApplicationBuilder app)
{
app.UseAuthentication();
app.Use(async (context,next) =>
{
var principal = new ClaimsPrincipal();
var result1 = await context.AuthenticateAsync("myauth1");
if (result1?.Principal != null)
{
principal.AddIdentities(result1.Principal.Identities);
}
var result2 = await context.AuthenticateAsync("myauth2");
if (result2?.Principal != null)
{
principal.AddIdentities(result2.Principal.Identities);
}
context.User = principal;
await next();
});
// ...
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何从下拉列表中选择“其他”选项时验证所需的
- asp.net-core – ABP框架中的集成Windows身份验证
- asp.net-mvc-3 – MVC3 Html.BeginForm – 在RouteValueDic
- asp.net-mvc-3 – 如何修改MVC3视图页面中的body类
- asp.net-mvc-5 – MVC5中的域路由
- asp-classic – 哪里可以存储经典ASP的连接字符串?
- webforms – ASP.NET Web窗体(4.5)强类型模型绑定 – ListV
- asp.net – Visual Studio 2013更改现有项目的身份验证
- asp.net – 需要安装Dnx Runtime软件包 有关详细信息,请参阅
- 用ASP.net检测iPad
