問題描述
我正在嘗試驗證 Azure AD 和 Graph 的 Intranet(基于 Orchard CMS),這在我的本地計算機(jī)上按預(yù)期運行,但是,當(dāng)訪問將成為生產(chǎn)站點時(已經(jīng)設(shè)置了 ssl我們內(nèi)部的dns),有時會出現(xiàn)上述錯誤,相對不一致,我部門的其他人在訪問時通常會出現(xiàn)此錯誤.
I'm attempting to authenticate for Azure AD and Graph for an Intranet (Based off Orchard CMS), this functions as expected on my local machine, however, when accessing what will be the production site (already set up with ssl on our internal dns), I get the above error at times, it's relatively inconsistent, others in my department while accessing usually get this error.
我的認(rèn)證控制器如下:
public void LogOn()
{
if (!Request.IsAuthenticated)
{
// Signal OWIN to send an authorization request to Azure.
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
public void LogOff()
{
if (Request.IsAuthenticated)
{
ClaimsPrincipal _currentUser = (System.Web.HttpContext.Current.User as ClaimsPrincipal);
// Get the user's token cache and clear it.
string userObjectId = _currentUser.Claims.First(x => x.Type.Equals(ClaimTypes.NameIdentifier)).Value;
SessionTokenCache tokenCache = new SessionTokenCache(userObjectId, HttpContext);
HttpContext.GetOwinContext().Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
SDKHelper.SignOutClient();
HttpContext.GetOwinContext().Authentication.SignOut(
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
我的openid選項配置如下:
My openid options are configured as follows:
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
var openIdOptions = new OpenIdConnectAuthenticationOptions
{
ClientId = Settings.ClientId,
Authority = "https://login.microsoftonline.com/common/v2.0",
PostLogoutRedirectUri = Settings.LogoutRedirectUri,
RedirectUri = Settings.LogoutRedirectUri,
Scope = "openid email profile offline_access " + Settings.Scopes,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
},
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = async (context) =>
{
var claim = ClaimsPrincipal.Current;
var code = context.Code;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
TokenCache userTokenCache = new SessionTokenCache(signedInUserID,
context.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase).GetMsalCacheInstance();
ConfidentialClientApplication cca = new ConfidentialClientApplication(
Settings.ClientId,
Settings.LogoutRedirectUri,
new ClientCredential(Settings.AppKey),
userTokenCache,
null);
AuthenticationResult result = await cca.AcquireTokenByAuthorizationCodeAsync(code, Settings.SplitScopes.ToArray());
},
AuthenticationFailed = (context) =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
}
}
};
var cookieOptions = new CookieAuthenticationOptions();
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(cookieOptions);
app.UseOpenIdConnectAuthentication(openIdOptions);
在 apps.dev.microsoft.com 和我們本地化的網(wǎng)絡(luò)配置中,重定向的 url 保持一致.
The url for redirection is kept consistent both at apps.dev.microsoft.com and in our localized web config.
推薦答案
就我而言,這是一個非常奇怪的問題,因為并不是每個人都會遇到這種問題,只有少數(shù)客戶和開發(fā)人員會遇到這個問題.
In my case, this was a very weird problem because it didn't happen in for everyone, only few clients and devs have this problem.
如果您僅在 chrome(或具有相同引擎的瀏覽器)中遇到此問題,您可以嘗試將 chrome 上的此標(biāo)志設(shè)置為禁用.
If you are having this problem in chrome only (or a browser that have the same engine) you could try setting this flag on chrome to disabled.
這里發(fā)生的情況是 chrome 具有不同的安全規(guī)則,即如果在沒有 Secure 屬性的情況下設(shè)置了沒有 SameSite 限制的 cookie,它將被拒絕".所以你可以禁用這個規(guī)則,它會起作用.
What happens here is that chrome have this different security rule that " If a cookie without SameSite restrictions is set without the Secure attribute, it will be rejected". So you can disable this rule and it will work.
或者,您也可以設(shè)置 Secure 屬性,但我不知道該怎么做;(
OR, you can set the Secure attribute too, but I don't know how to do that ;(
這篇關(guān)于IDX21323 OpenIdConnectProtocolValidationContext.Nonce 為空,OpenIdConnectProtocolValidatedIdToken.Payload.Nonce 不為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!