通过Azure AD认证用户时获得NULL身份认证

问题描述 投票:0回答:1

我正在尝试使用WS-federation通过Azure AD验证用户。

我已经实现了多个认证方案,并使用Challenge()将用户重定向到各自的方案。

return Challenge(new AuthenticationProperties { RedirectUri = "http://localhost:57826/Account/AzureADLogin"}, authenticationScheme);

这可以将我重定向到微软登录页面,登录成功后,它将我重定向到AzureADLogin()的操作方法。

但不知为何在AzureADLogin()中,我无法在该方法中获取登录用户身份(User.Identity.Name)。我在响应中收到的是空的请求。

enter image description here

此外,在Azure AD RedirectURIs设置为"http:/localhost:57826AccountAzureADLogin。".

有没有人知道我做错了什么或遗漏了什么?

asp.net-core azure-active-directory single-sign-on ws-federation
1个回答
0
投票

我刚刚和这个问题斗争了3天多,我在3月份第一次注意到这个问题时,花了好几天时间排除故障和重写我的应用程序。 在我的情况下,我有一个.Net 4.5.2应用程序,在使用WSFederation和OWIN登录Azure AD时,它已经工作了多年。 然而,在2020年3月发生了服务器Win 2016的更新,破坏了登录。 每一次我得到登录用户名的地方在System.Web.HttpContext.User.Identity.Name中都是空白的,而在更新之前,每次都已经填充了。 我追溯到在服务器2016的KB4537764的更新中。(仅供参考,2012年的更新是KB4532946和KB4532940)如果我删除了更新,登录工作。 再次应用更新,就坏了。 我上网搜索KB4537764,直到脸色发青,总是一无所获。 直到我研究了KB4537764的实际作用--它提到了对SameSite的修复--我才找到了我需要做的事情来修复它,并在服务器上安装更新。

  • 在Nuget包管理器中更新所有的OWIN版本到4.1.0。
  • 你需要将应用程序配置为使用SystemWebCookieManager。 在starttup.cs中添加这个。 我在Configuration(IAppBuilder app)中把它作为第一行。

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        // ...
        CookieManager = new SystemWebCookieManager()
    });
    
  • 在Startup.cs中添加使用Microsoft.Owin.Host.SystemWeb和使用Microsoft.Owin.Security.Cookies来编写上述代码。
  • 在我的PageAuthorize类中,如果System.Web.HttpContext.Current.User.Identity.Name为空白,我将进行SSO调用。

    public class PageAuthorize : AuthorizeAttribute{ }。

    private readonly string[] allowedroles;         
    public PageAuthorize(params string[] roles)
    {
        this.allowedroles = roles;
    
    }
    
    
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        bool authorize = false;      
    
        if (System.Web.HttpContext.Current.User.Identity.Name=="")
         {
     System.Web.HttpContext.Current.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "https://mysite/Home/LoggedIn" }, WsFederationAuthenticationDefaults.AuthenticationType);  
            authorize = true;
        }
        else
        {
            foreach (var role in allowedroles)
            {
                int intLoginId;
                string staffName;
                sysRole sysRole;
    
                var _staffService = DependencyResolver.Current.GetService<IStaffService>();
                LoginMySelf me = new LoginMySelf(_staffService);
                var Login = me.DetermineLogin(out intLoginId, out staffName, out sysRole);
    
                //set session vars
                var _sessionManagerService = DependencyResolver.Current.GetService<ISessionManagerService>();
                string strSess = _sessionManagerService.SetLoginSessionVars(Login, intLoginId, "SSO", sysRole.RoleName,
                       staffName, Convert.ToInt32(sysRole.AccessLevelValue));
                System.Web.HttpContext.Current.Session["RoleName"] = sysRole.RoleName;
                System.Web.HttpContext.Current.Session["StaffName"] = staffName;
    
                if (role == sysRole.RoleName)
                {
                    authorize = true;
                }
            }
    
        }
    
        return authorize;
    }
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        HttpContext.Current.Response.Redirect("~/Home/NotAuthorized");
    }
    

    }

  • 现在System.Web.HttpContext.Current.User.Identity.Name总是像预期的那样包含我的用户名。

以下是我用来隔离和修复问题的参考文献:--------。https:/github.comaspnetAspNetKatanaissues324。-https:/github.comaspnetAspNetKatanawikiSystem.Web-response-cookie-integration-issues。

希望对你和其他人有帮助。 这绝对是令人疯狂的诊断,但修复方法很简单!

© www.soinside.com 2019 - 2024. All rights reserved.