gpt4 book ai didi

identityserver3 - 使用 Resource Owner Flow 发布 cookie

转载 作者:行者123 更新时间:2023-12-04 06:55:37 26 4
gpt4 key购买 nike

我已经通读了类似的问题,但没有找到我的用例。我们有一个使用 AspNetIdentity 表单例份验证的旧 MVC 应用程序。我们开始将网站的部分内容转移到使用 IdentityServer3 IdP 的新系统。从用户的角度来看,他们登录到“旧”站点。进一步进入站点,他们可能会被重定向到新站点。目前,当发生这种情况时,他们必须再次登录到 SSO 页面以获取新的 SSO cookie。我们正在努力缓解双重登录。

在后端,用户帐户在两个身份验证数据库之间同步,因此他们的用户名和密码始终保持同步。为了减少双重登录,我试图在“旧”站点的登录页面上使用资源所有者流程,以便它首先执行 Forms Auth 登录,然后使用资源所有者将凭据传递给 IdentityServer 以使用新系统进行身份验证。我想如果此时发生 SSO 服务器身份验证,则可以构建一个 cookie,以便稍后当他们导航到受 SSO 保护的站点时,他们已经拥有一个有效的 cookie。我发现的问题是 Resource Owner 方法似乎没有发出 cookie。我在执行资源所有者身份验证的 IdentityServer 托管网站上创建了一个 MVC Controller 。我让“旧”站点为此页面打开一个新的浏览器选项卡,以便在发出 Set-Cookie 时 HttpGet Controller 响应将返回到浏览器。资源所有者身份验证有效,我获得了访问 token ,并且 userInfo 也有效。我使用 OWIN 身份验证管理器构建了 ClaimsIdentity 和 SignIn。声明主体显示用户已通过身份验证,但我没有收到 cookie。有什么办法可以做到这一点。

总而言之,在“旧”系统登录时使用他们的明文凭据,我想使用 SSO 对他们进行预验证以获取 cookie,这样以后导航到 SSO 站点时就不会要求登录。

这是资源所有者 Auth Get Controller 。

public ActionResult LoginGet(string username, string password) {
try {
username = HttpUtility.UrlDecode(username);
password = HttpUtility.UrlDecode(password);
//create identityserver sso cookie as well
var tokenClient = new TokenClient(
"https://localhost.fiddler:44333/core/connect/token",
"clientId",
"secret"
);
var scopes = "openid profile sampleApi roles";
var token = tokenClient.RequestResourceOwnerPasswordAsync(username, password, scopes).Result;
if (token != null && !String.IsNullOrWhiteSpace(token.AccessToken)) {
var claims = new List<Claim>();
var claimsClient = new UserInfoClient(new Uri("https://localhost.fiddler:44333/core/connect/userinfo"), token.AccessToken);
var userInfo = claimsClient.GetAsync().Result;
userInfo.Claims.ToList().ForEach(t => claims.Add(new Claim(t.Item1, t.Item2)));
claims.Add(new Claim("token", token.AccessToken));
var claimsId = new ClaimsIdentity(claims, "Cookies");
Request.GetOwinContext().Authentication.SignIn(claimsId);
ViewBag.Worked = true;
ClaimsPrincipal cp = Request.GetOwinContext().Authentication.User;
if(cp != null) {
ViewBag.IsAuthed = cp.Identity.IsAuthenticated;
}
return View();
}
} catch (Exception ex) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return View();
}

最佳答案

API/反向 channel 调用中对 token 端点的调用。要设置 SSO cookie,您必须将浏览器重定向到 IdentityServer。

IOW - 如果没有浏览器重定向,你想做的事情是不可能的。

关于identityserver3 - 使用 Resource Owner Flow 发布 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40590753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com