首页 > 解决方案 > 无法在 MVC 应用程序中使用 Microsoft 身份平台授权用户,我正在获取 cookie 但它没有返回用户信息

问题描述

这是单击登录按钮时调用的函数

public void O365Login() {
            if (!Request.IsAuthenticated)
            {
                HttpContext.GetOwinContext().Authentication.Challenge(
                    new AuthenticationProperties { RedirectUri = "/" },
                    OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }
        }

我的重定向 URL 是http://localhost/Home/OfficeLogin

    [HttpPost]
        public ActionResult OfficeLogin()
        {
            if (System.Security.Claims.ClaimsPrincipal.Current.FindFirst("preferred_username") != null)
            {
                UserDetail ud = new UserDetail();
                ud.EmailId = System.Security.Claims.ClaimsPrincipal.Current.FindFirst("preferred_username").Value;
//some more items to do after getting the email address
            }
            return View(ViewBag);
        }

到目前为止,这一直运作良好,但本周突然停止运作。

当我用它装饰任何控制器时,[Authorize]它只会一次又一次地回到 Office 365 登录页面

使用我的 O365 帐户登录后,我每次都会收到一个 cookie,如下所示

OpenIdConnect.nonce.3U+OSOW9r7Hil3ld93kUSryN/mbVs2l2j4kW94KLSOc=

但它没有返回任何用户信息,如姓名/电子邮件等,并且 request.IsAuthorized 仍然是错误的

标签: c#asp.net-mvcoffice365microsoft-identity-platform

解决方案


推荐阅读