以编程方式登录到identityserver3 [英] programmatically sign in to identityserver3

查看:78
本文介绍了以编程方式登录到identityserver3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方网站,该网站将我的网站嵌入其中,并且我的网站通过URL参数中的某些键来验证第三方.

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.

第三方用户使用自己的身份验证模型登录(因为他们不与我的网站进行SSO或联合身份验证),并且我的网站与由IdentityServer3实现的我自己的身份提供程序兼容.

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.

问题是:我可以通过模仿的用户以编程方式登录到我的idp吗?好像第三方用户登录到他们的网站并访问我的嵌入式网站一样,那么我的网站会自动以假冒用户身份登录到我的idp,并且我的网站会显示给第三方用户?

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?

互动关系图

推荐答案

是的

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

这篇关于以编程方式登录到identityserver3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆