c# - 如何使用环回 IP 检索身份验证 token

标签 c# asp.net-mvc authentication loopback

最近 Google 禁止了嵌入式浏览器身份验证,转而支持 Loopback IP方法

我的网站有一个桌面客户端,它使用与网站相同的登录系统。基本上我会在我的网站上弹出一个嵌入式浏览器 View ,并在登录完成后获取 cookie。然后,桌面客户端将使用我网站的身份验证 cookie 进行 API 访问。

既然 Google 禁止嵌入式浏览器,我需要一种新方法(如果我想保持 Google 外部登录)。

如果我理解正确,那么新流程将类似于
1.桌面客户端监听localhost::port
2. 桌面客户端启动网站登录页面的 URL 并打开用户的默认浏览器
3. 用户使用自己喜欢的浏览器登录网站
4. Web 服务器检测到登录成功,并以某种方式将带有身份验证 token 的重定向发送到 localhost::port
5.桌面客户端从localhost读取token

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

最佳答案

找到了两种方法来做到这一点。一种是使用 TcpListener 并绑定(bind)到 Loopback 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());

生成 token

    TimeSpan tokenExpiration = TimeSpan.FromDays(1);

    ClaimsIdentity identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
    identity.AddClaim(new Claim(ClaimTypes.Name, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5b7a5b14595557" rel="noreferrer noopener nofollow">[email protected]</a>"));
    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 将始终显示您网站的域。

关于c# - 如何使用环回 IP 检索身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56263298/

相关文章:

@Url.action() 的 ASP.NET MVC 帖子

c# - HttpClient 没有使用客户端证书进行相互 TLS 身份验证

python - 使用 Python 中的 requests 模块登录巴克莱 super 联赛梦幻足球?

c# - 如何使用 Visual Studio + ReSharper 禁用不需要的自动完成功能?

c# - 如何使用lucene搜索单词的一部分?

c# - 关于从 ASP.NET 2.0 到 ASP.NET 3.5 MVC 的区别的问题

javascript - Kendo 在初始文件选择后上传复制文件

c# - 无法创建 Amazon Web Services (AWS) 凭据对象

c# - 从 Oracle 到 MySql 数据库的 DateTime 值

php - 管理员和用户登录php mysql表单