相关疑难解决方法(0)

如何在ASP.NET Core中创建自定义AuthorizeAttribute?

我正在尝试在ASP.NET Core中创建自定义授权属性.在以前的版本中,可以覆盖bool AuthorizeCore(HttpContextBase httpContext).但这不再存在于AuthorizeAttribute.

制作自定义AuthorizeAttribute的当前方法是什么?

我想要完成的任务:我在Header Authorization中收到一个会话ID.从该ID我将知道特定动作是否有效.

c# asp.net authorization asp.net-core-mvc asp.net-core

362
推荐指数
13
解决办法
19万
查看次数

如何在ASP.NET Core 2.0中设置多个身份验证方案?

我正在尝试将我的auth资源迁移到Core 2.0并使用我自己的身份验证方案出现问题.我在启动时的服务设置如下所示:

var authenticationBuilder = services.AddAuthentication(options =>
{
    options.AddScheme("myauth", builder =>
    {
        builder.HandlerType = typeof(CookieAuthenticationHandler);
    });
})
    .AddCookie();
Run Code Online (Sandbox Code Playgroud)

我在控制器中的登录代码如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

但是当我在控制器或动作过滤器中时,我只有一个身份,而且它不是经过身份验证的身份:

var identity = context.HttpContext.User.Identities.SingleOrDefault(x => x.AuthenticationType == "myauth");
Run Code Online (Sandbox Code Playgroud)

导航这些变化一直很困难,但我猜我正在做.AddScheme错了.有什么建议?

编辑:这里(基本上)是一个干净的应用程序,不会在User.Identies上产生两组身份:

namespace WebApplication1.Controllers
{
    public class Testy : Controller
    {
        public IActionResult Index()
        {
            var i = HttpContext.User.Identities; …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-core asp.net-core-2.0

30
推荐指数
3
解决办法
2万
查看次数