简体   繁体   English

为什么在iframe中使用.net core 2 AuthorizationHandler时身份为null

[英]Why is identity null when using .net core 2 AuthorizationHandler in iframe

I have the following AuthorizationRequirement and AuthorizationHandler that is registered with DI and is working just fine. 我有以下向DI注册的AuthorizationRequirementAuthorizationHandler ,并且工作正常。 However, when an action with [Authorize(Policy = "ConfirmedEmail")] is called within an iframe (same origin), context.User.Identity.Name is allways NULL and therefore so is user . 但是,在iframe(相同来源)中调用带有[Authorize(Policy = "ConfirmedEmail")]操作时, context.User.Identity.Name始终为NULL,因此user Does anyone have any idea why this is, and more importantly how to fix it? 有谁知道这是为什么,更重要的是如何解决?

When the exact same action is called directly (outside an iframe), context.User.Identity.Name is correct and the user lookup succeeds. 当直接调用完全相同的操作(在iframe外部)时, context.User.Identity.Name是正确的,并且用户查找成功。

public class ConfirmedEmailRequirement : IAuthorizationRequirement { }

    public class ConfirmedEmailHandler : AuthorizationHandler<ConfirmedEmailRequirement>
    {
        private readonly UserManager<User> _userManager;

        public ConfirmedEmailHandler(UserManager<User> userManager)
        {
            _userManager = userManager;
        }

        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, ConfirmedEmailRequirement requirement)
        {
            var user = await _userManager.GetUserAsync(context.User);
            if (user?.EmailConfirmed == true)
            {
                context.Succeed(requirement);
            }
            else
            {
                context.Fail();
            }
        }
    }

UPDATE : I have noticed that although both iframe and non iframe request have the same session cookie, the non iframe request includes a Core.Identity.Application cookie, the iframe request does not. 更新 :我注意到,尽管iframe和非iframe请求都具有相同的会话cookie,但非iframe请求包含Core.Identity.Application cookie,但iframe请求却没有。 I don't know the significance of this or what is causing it. 我不知道此问题的重要性或原因。

I finally managed to get identity to work within an iframe using the following: 我终于设法使用以下方法在iframe中使用身份:

services.ConfigureApplicationCookie(options => {
    options.Cookie.Name = "MyAuthCookie";
    options.Cookie.SameSite = SameSiteMode.None; //<THIS!!!
});

Hope this saves someone a lot of head scratching some day. 希望这可以节省某人每天挠头的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试使用 ASP.NET 核心标识创建新用户时,为什么我的 ApiKey 变量位于 null 中? - Why can it be that my ApiKey variable is in null when I try to create a new user using ASP.NET Core Identity? 如何从 AuthorizationHandler .NET Core 获取参数 - How to get params from AuthorizationHandler .NET Core ASP.NET 核心 AuthorizationHandler 未被调用 - ASP.NET Core AuthorizationHandler not being called 为什么使用ASP.NET Identity Core时OAuth会失败? - Why does OAuth fail when using ASP.NET Identity Core? 使用ASP.NET Core身份验证和基于令牌的身份验证时,为什么要登录新用户? - Why sign-in a new user when using ASP.NET Core Identity and token-based auth? ASP.NET Core(3.0) 上的身份在使用端点时不起作用 - Identity on ASP.NET Core(3.0) not functioning when using endpoints .NET Core AuthorizationHandler 失败 - 如何路由到新页面 - .NET Core AuthorizationHandler fails - how to route to a new page .NET Core项目中添加Identity时出现NullReferenceException - NullReferenceException when adding Identity in .NET Core project 将 ASP.NET 核心标识与 MongoDB 一起使用 - Using ASP.NET Core Identity with MongoDB User.Identity.GetUserId() 在 .net Core 中总是返回 null - User.Identity.GetUserId() always returns null in .net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM