我正在尝试为Intranet (基于Orchard CMS)验证Azure AD和Graph,这在我的本地计算机上的功能与预期相同,但是,当访问将成为生产站点的站点(已经在内部dns上使用ssl设置)时,我有时会收到上述错误,这是相对不一致的,我部门中的其他人在访问时通常会收到此错误。
我的认证控制器如下所示:
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选项配置如下:
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);用于重定向的url在apps.dev.microsoft.com和我们本地化的web配置中都保持一致。
发布于 2020-08-04 02:47:53
在我的例子中,这是一个非常奇怪的问题,因为它不是每个人都会发生的,只有少数客户端和开发人员有这个问题。
如果你只在chrome (或者有相同引擎的浏览器)中遇到这个问题,你可以尝试在chrome上设置这个标志为disabled。

这里发生的事情是,chrome有这个不同的安全规则,“如果没有SameSite限制的cookie在没有安全属性的情况下被设置,它将被拒绝”。因此,您可以禁用此规则,它将正常工作。
或者,你也可以设置Secure属性,但是我不知道怎么做;
发布于 2018-10-24 19:14:47
检查AD应用程序注册-->设置-->回复网址中提到的网址。例如,如果该网址为https://localhost:44348/
转到MVC项目-->属性(右键单击并属性) --> Web部分-->开始URL和项目URL也应为https://localhost:44348/
这为我解决了这个问题。另一个选项是在Startup.Auth中进行AD身份验证后动态设置重定向URL
发布于 2019-01-03 18:02:45
这行代码解决了这个问题,错误的原因是ASP.NET还没有创建会话信息。函数"authFailed.OwinContext.Authentication.Challenge()“用身份验证所需的信息填充报头。
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthenticationFailed = AuthenticationFailedNotification<OpenIdConnect.OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> authFailed =>
{
if (authFailed.Exception.Message.Contains("IDX21323"))
{
authFailed.HandleResponse();
authFailed.OwinContext.Authentication.Challenge();
}
await Task.FromResult(true);
}
}
});https://stackoverflow.com/questions/49944071
复制相似问题