首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用回环IP检索auth令牌

如何使用回环IP检索auth令牌
EN

Stack Overflow用户
提问于 2019-05-22 18:57:20
回答 1查看 1.1K关注 0票数 2

最近,谷歌禁止使用嵌入式浏览器来支持回环IP方法。

我的网站有一个桌面客户端,它使用与网站相同的登录系统。基本上,我弹出一个嵌入式浏览器视图到我的网站,并抓取饼干后,登录完成。然后,我的站点的auth cookie将用于桌面客户端的API访问。

现在Google禁止嵌入式浏览器,我需要一种新的方法(如果我想维护Google外部登录)。

如果我正确地理解了它,那么新的过程就会像

  1. 桌面客户端侦听localhost::port
  2. 桌面客户端启动网站登录页面的Url并打开用户的默认浏览器
  3. 用户使用他们最喜欢的浏览器登录网站
  4. web服务器检测登录成功,并以某种方式向localhost::port发送带有auth令牌的重定向。
  5. 桌面客户端从本地主机读取令牌。

但是,如果重定向URL是localhost,如何显示登录成功页面以指示用户关闭浏览器并返回到桌面应用程序?我应该如何在服务器和桌面客户机上实现这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-23 21:55:45

找到了两种方法。一种是使用TcpListener并绑定到回收站IP。响应以流的形式返回,您需要进一步解析它才能获得所需的数据,这是一个巨大的痛苦。

另一种方法是使用HttpListener

代码语言:javascript
运行
复制
        using (HttpListener listener = new HttpListener())
        {
            listener.Prefixes.Add("http://localhost:{port}/"); //Only listen to this particular address
            listener.Start();

            //blocking call. Feel free to use async version
            HttpListenerContext context = listener.GetContext(); 
            HttpListenerRequest request = context.Request;

            HttpListenerResponse response = context.Response;

            //Here is the response url. The token should be inside url query param.
            Console.WriteLine(request.Url); 

            //redirects user back to your site and show a login success screen
            response.Redirect("{yourdomain}/loginsuccess");
            //Important! call close to send out the response
            response.Close();

            //Important! If listener is stopped before response is sent out then it will abort.
            Thread.Sleep(1000);
            listener.Stop();
        }

在服务器端,只需在登录完成后将用户重定向到http://localhost:{port}/?token=xxxx

asp.net实现:

Startup.Auth.cs中添加一个静态字段

代码语言:javascript
运行
复制
public static TicketDataFormat AccessTokenFormat;

ConfigureAuth

代码语言:javascript
运行
复制
AccessTokenFormat = new TicketDataFormat(app.CreateDataProtector(typeof(OAuthAuthorizationServerMiddleware).Namespace, "Access_Token", "v1"));

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

生成令牌

代码语言:javascript
运行
复制
    TimeSpan tokenExpiration = TimeSpan.FromDays(1);

    ClaimsIdentity identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
    identity.AddClaim(new Claim(ClaimTypes.Name, "a@a.com"));
    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "999"));

    AuthenticationProperties props = new AuthenticationProperties()
    {
        IssuedUtc = DateTime.UtcNow,
        ExpiresUtc = DateTime.UtcNow.Add(tokenExpiration),
    };

    AuthenticationTicket ticket = new AuthenticationTicket(identity, props);

    string accessToken = Startup.AccessTokenFormat.Protect(ticket);

使用这种方法,用户甚至不会注意到我们已经将他/她调调到本地主机。浏览器的URL将始终显示您网站的域名。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56263298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档