以编程方式登录identityserver3

时间:2016-12-03 16:24:23

标签: identityserver3

我有第三方网站,我的网站嵌入其中,我的网站通过URL参数中的一些键验证第三方。

第三方用户使用自己的身份验证模式登录(因为他们不与我的网站进行SSO或联盟),我的网站使用IdentityServer3实现的我自己的身份提供商。

问题是:我可以通过编程方式使用模拟用户登录我的idp吗?好像第三方用户登录他们的网站,访问我的嵌入式网站,然后我的网站自动登录我的idp与模拟用户,我的网站显示给第三方用户?

diagram for the interaction

1 个答案:

答案 0 :(得分:0)

是的,你可以

            var client = new HttpClient();
        var dic = new Dictionary<string, string>();
        dic.Add("client_id", "mvc");
        dic.Add("client_secret", "secret");
        dic.Add("grant_type", "password");
        dic.Add("scope", "openid profile");
        dic.Add("username", "yazan@catec.ae");
        dic.Add("password", "P@ssword1");

        var content = new FormUrlEncodedContent(dic);

        var msg = client.PostAsync("https://localhost:44383/identity/connect/token", content).Result.Content.ReadAsStringAsync().Result;
        string token = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(msg).access_token;

        var jwt = new JwtSecurityToken(token);
        var identity = new ClaimsIdentity("ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
        foreach (var c in jwt.Claims)
        {
            var t = c.Type;
            var v = c.Value;

            identity.AddClaim(new Claim(t, v));

        }
            IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
            authenticationManager.SignOut("ApplicationCookie");
            authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

        return Redirect("Index");