programmatically sign in to identityserver3

空扰寡人 提交于 2019-12-08 10:25:10

问题


I have 3rd party website which embeds my website inside it and my website validates the 3rd Party through some keys in the URL parameters.

3rd party user login with their own authentication model (as they don't do SSO or federation with my website) and my website works with my own identity provider implemented by IdentityServer3.

The question is: Can I sign in with the impersonated user to my idp programmatically? As if 3rd party users login to their website, and accessing my embedded website, then my website automatically sign in to my idp with impersonated user and my website is shown to 3rd party user?

diagram for the interaction


回答1:


Yes you can

            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");


来源:https://stackoverflow.com/questions/40949893/programmatically-sign-in-to-identityserver3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!