Cookies 身份服务器和用户模拟

Cookies 身份服务器和用户模拟,cookies,identityserver3,Cookies,Identityserver3,我有两个网站(用户网站)和(管理员网站),我使用Identity Server 3进行访问控制,这是托管在 这些站点在identity server中配置为具有基于cookie的身份验证的同一客户端(不同的重定向URL)。我想提供一种机制,让管理员站点的用户可以模拟用户站点的用户 我已经看到,我可以使用IssueLoginCookie发布Cookie,但该调用需要在identity server上进行,因此鉴于它位于另一个域上,我看不出该如何工作 如何在identity server中支持用户模

我有两个网站(用户网站)和(管理员网站),我使用Identity Server 3进行访问控制,这是托管在

这些站点在identity server中配置为具有基于cookie的身份验证的同一客户端(不同的重定向URL)。我想提供一种机制,让管理员站点的用户可以模拟用户站点的用户

我已经看到,我可以使用
IssueLoginCookie
发布Cookie,但该调用需要在identity server上进行,因此鉴于它位于另一个域上,我看不出该如何工作

如何在identity server中支持用户模拟

更新

我现在让管理员站点生成如下url:

var url = 'http://localhost:61826/connect/authorize'
    + '?state=' + encodeURIComponent(((Date.now() + Math.random()) * Math.random()).toString().replace(".", ""))
    + '&nonce=' + encodeURIComponent(((Date.now() + Math.random()) * Math.random()).toString().replace(".", ""))
    + '&client_id=mvc'
    + '&redirect_uri=' + encodeURIComponent('http://localhost:64822/')
    + '&scope=' + encodeURIComponent('openid profile roles api1')
    + '&acr_values=' + encodeURIComponent('loginas:3230')
    + '&response_type=' + encodeURIComponent('id_token token')
    + '&prompt=login';

window.location.href = url;
这允许我在自定义
IUserService
上的
PreAuthenticateAsync
方法中拾取登录事件并拦截登录。我目前的做法是:

public override async Task PreAuthenticateAsync(PreAuthenticationContext context)
{
    if (context.SignInMessage.AcrValues.Any(acr => acr.StartsWith("loginas:")))
    {
        // Would need to also ensure that the user has the relevant persmissions to impersonate another user
        var subjectId = _owinContext.Authentication.User.GetSubjectId();

        var login = new AuthenticatedLogin
        {
            Name = "Impersonating For Fun",
            Subject = "3230",
            Claims = new List<Claim>
            {
                new Claim(Constants.ClaimTypes.Subject, "3230")
            },
            PersistentLogin = true,
            IdentityProvider = Constants.BuiltInIdentityProvider,
            AuthenticationMethod = "Cookies"
        };

        _owinContext.Environment.IssueLoginCookie(login);

        var impersonationClaims = new List<Claim>
        {
            new Claim("AdminUserId", subjectId)
        };

        context.AuthenticateResult = new AuthenticateResult("3230", "Impersonating For Fun", impersonationClaims);
    }

    await Task.FromResult(0);
}
public override异步任务预验证异步(预验证上下文)
{
if(context.SignInMessage.AcrValues.Any(acr=>acr.StartsWith(“loginas:”))
{
//还需要确保用户具有模拟其他用户的相关权限
var subjectId=_owinContext.Authentication.User.GetSubjectId();
var login=新的AuthenticatedLogin
{
Name=“模拟娱乐”,
Subject=“3230”,
索赔=新名单
{
新索赔(Constants.ClaimTypes.Subject,“3230”)
},
PersistentLogin=true,
IdentityProvider=Constants.BuiltInIdentityProvider,
AuthenticationMethod=“Cookies”
};
_owinContext.Environment.IssueLoginCookie(登录);
var impersonationClaims=新列表
{
新声明(“AdminUserId”,主体)
};
context.AuthenticateResult=新的AuthenticateResult(“3230”,“模拟娱乐”,模拟声明);
}
等待任务。从结果(0);
}

用户未显示登录页面,并且已正确重定向到目标url。但是,用户没有更改为新用户,而是保持为原始用户。我遗漏了什么?

您正在设置域范围的cookie吗?您能在浏览器中确认吗