我有兩個 MVC 應用程式 AppA 和 AppB,并為登錄實作了 Azure AD 身份驗證。我能夠成功登錄這兩個應用程式。但問題是,在我登錄到 AppA 然后到 AppB 之后,在我回傳 AppA 的某個時間之后,我面臨用戶已注銷的問題,它再次重定向到登錄螢屏(在 AppA 中)。在我登錄 AppA(第二次)并回傳 AppB 后(AppB 中的用戶已注銷)。
客戶端 ID 不同;TenandID 是一樣的。兩個應用程式都托管在同一服務器中。
啟動檔案:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
SlidingExpiration = true,
Provider = new CookieAuthenticationProvider
{
OnResponseSignIn = context =>
{
context.Properties.AllowRefresh = true;
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1);
},
OnValidateIdentity = MyCookieValidateIdentity
},
ExpireTimeSpan = TimeSpan.FromDays(2)
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = appId,
//CookieManager=new SameSiteCookieManager(new SystemWebCookieManager()),
Authority = "https://login.microsoftonline.com/xxxxxx/v2.0",
Scope = $"openid email profile offline_access {graphScopes}",
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
},
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = (context) =>
{
context.ProtocolMessage.DomainHint = "xyz.com";
return Task.FromResult(0);
},
// SecurityTokenValidated = OnSecurityTokenValidated,
AuthenticationFailed = OnAuthenticationFailedAsync,
AuthorizationCodeReceived = OnAuthorizationCodeReceivedAsync
}
}
);
}
actionContext.RequestContext.Principal.Identity.IsAuthenticated 回傳 False
我假設它必須對 cookie 做一些事情。有人可以幫忙解決這個問題嗎?
編輯:進一步除錯并發現:最初如果 AppA 的 cookie 設定為: .AspNet.Cookies = A_abc123 ; ASP.NET_SessionId = A_def456 而對于 AppB .AspNet.Cookies = B_mno123 ; ASP.NET_SessionId = B_pqr456 然后在我單擊 AppA 中的任何鏈接后,cookie 的值將使用 AppB 的 cookie 進行更新,即.AspNet.Cookies = B_mno123 ;ASP.NET_SessionId = B_pqr456
.AspNet.Cookies ASP.NET_SessionId
AppA A_abc123 A_def456
AppB B_mno123 B_pqr456
AppA B_mno123 B_pqr456
uj5u.com熱心網友回復:
您需要做的一件事是配置資料保護 API,以便兩個服務使用相同的 cookie 保護密鑰。開箱即用的每個服務都會創建自己的唯一密鑰,并且來自一個服務的 cookie 在不同的服務中無效。
我還在這里寫了一篇關于資料保護 API 的博客文章。
看
- 如何:使用資料保護
- 開始使用 ASP.NET Core 中的資料保護 API
uj5u.com熱心網友回復:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
//AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,// DefaultAuthenticationTypes.ApplicationCookie,
CookieName = ".AspNet.AppA.Cookies",
SlidingExpiration = true,
CookieManager = new SystemWebCookieManager(),
Provider = new CookieAuthenticationProvider
{
OnResponseSignIn = context =>
{
context.Properties.AllowRefresh = true;
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1);
},
},
ExpireTimeSpan = TimeSpan.FromDays(2)
});
//... code removed for brevity //
}
應用程式設定的默認 Cookie 名稱是:.AspNet.Cookies 當我修改默認 cookie 名稱時,問題得到了解決。每個應用程式都在生成自己的 cookiename,因此其他應用程式沒有注銷用戶。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/363949.html
