System.Security.Principal.WindowsIdentity (User.Identity) 无法



我已经使用OpenIdConnect for Azure AD身份验证。

我的应用程序是多租户的。我已使用 Azure AD 进行应用程序身份验证。

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions { });
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {   ..other fields..   }

成功登录后,在生成并启动应用程序后,它工作正常。声明主体已正确填充了 Azure AD 发送的数据。

当同一应用程序在不同的浏览器中运行时,它非常无法填充。

恳请建议。

提前感谢,拉胡尔

我的应用程序中的问题是由于我在应用程序中使用的中间件 Owin 的行为。

欧文的问题:在 OWIN 中,响应标头集合是响应 Cookie 的主要存储位置。但是,System.Web 将响应 cookie 存储在单独的 HttpContext.Response.Cookies 集合中,然后在发送响应之前将它们写出到 Response.Headers 集合中。如果对同一请求使用两种方法,如果 OWIN 都存在冲突,这可能会导致冲突,因为 Response.Cookies 集合将覆盖通过 OWIN 响应标头设置的任何 Cookie。

不幸的是,这个问题没有通用的解决方案。来自 OWIN 的 set-cookie 标头无法通过 System.Web 的 Response.Cookie 集合可靠地重新解析和重定向。默认情况下,OWIN 组件也不能直接写入 System.Web 的 Response.Cookie 集合,因为这会损害其平台独立性。

解决方法:重新配置 CookieAuthenticationMiddleware 以直接写入 System.Web 的 Cookie 集合

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            // ...
            CookieManager = new SystemWebCookieManager()
        });

SystemWebCookieManager 类定义如下:

public class SystemWebCookieManager : ICookieManager
{
    public string GetRequestCookie(IOwinContext context, string key)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        var webContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
        var cookie = webContext.Request.Cookies[key];
        return cookie == null ? null : cookie.Value;
    }
    public void AppendResponseCookie(IOwinContext context, string key, string value, CookieOptions options)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (options == null)
        {
            throw new ArgumentNullException("options");
        }
        var webContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
        bool domainHasValue = !string.IsNullOrEmpty(options.Domain);
        bool pathHasValue = !string.IsNullOrEmpty(options.Path);
        bool expiresHasValue = options.Expires.HasValue;
        var cookie = new HttpCookie(key, value);
        if (domainHasValue)
        {
            cookie.Domain = options.Domain;
        }
        if (pathHasValue)
        {
            cookie.Path = options.Path;
        }
        if (expiresHasValue)
        {
            cookie.Expires = options.Expires.Value;
        }
        if (options.Secure)
        {
            cookie.Secure = true;
        }
        if (options.HttpOnly)
        {
            cookie.HttpOnly = true;
        }
        webContext.Response.AppendCookie(cookie);
    }
    public void DeleteCookie(IOwinContext context, string key, CookieOptions options)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (options == null)
        {
            throw new ArgumentNullException("options");
        }
        AppendResponseCookie(
            context,
            key,
            string.Empty,
            new CookieOptions
            {
                Path = options.Path,
                Domain = options.Domain,
                Expires = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
            });
    }
}

它不再给我带来任何唱歌问题。

当应用程序使用集成身份验证进行身份验证时,才应使用 WindowsIdentity。但是,这里您使用的是OpenID Connect,因此您应该只使用ClaimsPrincipal和ClaimsIdentity。

相关内容

最新更新