最近,谷歌禁止使用嵌入式浏览器来支持回环IP方法。
我的网站有一个桌面客户端,它使用与网站相同的登录系统。基本上,我弹出一个嵌入式浏览器视图到我的网站,并抓取饼干后,登录完成。然后,我的站点的auth cookie将用于桌面客户端的API访问。
现在Google禁止嵌入式浏览器,我需要一种新的方法(如果我想维护Google外部登录)。
如果我正确地理解了它,那么新的过程就会像
localhost::port
localhost::port
发送带有auth令牌的重定向。但是,如果重定向URL是localhost,如何显示登录成功页面以指示用户关闭浏览器并返回到桌面应用程序?我应该如何在服务器和桌面客户机上实现这一点?
发布于 2019-05-23 21:55:45
找到了两种方法。一种是使用TcpListener
并绑定到回收站IP。响应以流的形式返回,您需要进一步解析它才能获得所需的数据,这是一个巨大的痛苦。
另一种方法是使用HttpListener
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
中添加一个静态字段
public static TicketDataFormat AccessTokenFormat;
在ConfigureAuth
中
AccessTokenFormat = new TicketDataFormat(app.CreateDataProtector(typeof(OAuthAuthorizationServerMiddleware).Namespace, "Access_Token", "v1"));
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
生成令牌
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将始终显示您网站的域名。
https://stackoverflow.com/questions/56263298
复制相似问题