【问题标题】:Connect OAuth Tokens with a user将 OAuth 令牌与用户连接
【发布时间】:2020-10-11 01:35:46
【问题描述】:

我正在尝试找出将令牌映射到用户的最佳方式。我想我遇到了一个常见的问题授权与身份验证。

我正在创建一个市场,我的支付服务由条带支持,因此我目前允许使用条带登录。

我像这样注册我的条带服务:

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})    
.AddOAuth<OAuthOptions, StripeConnectOAuthHandler<OAuthOptions>>(
    StripeConnectDefaults.AuthenticationScheme, options => {
    options.SaveTokens = true;
    options.ClientId = Configuration["Stripe:ClientId"];
    options.ClientSecret = Configuration["Stripe:ClientSecret"];
    options.TokenEndpoint = StripeConnectDefaults.TokenEndpoint;
    options.AuthorizationEndpoint = StripeConnectDefaults.AuthorizationEndpoint;
    options.UserInformationEndpoint = StripeConnectDefaults.UserInformationEndpoint;
    options.Scope.Add("read_write");
    options.CallbackPath = new PathString("/signin-stripeconnect");
    //...
});

由于我使用 stripe 来处理付款,因此我需要令牌来执行某些行为,例如创建付款事件或订阅一个付款事件,但我不想强制我的用户必须有一个 stripe 帐户才能查看数据我的网站。

所以我想添加其他登录方式,但我需要将这些用户链接在一起

app.UseGoogleAuthentication(new GoogleOptions()
{
    AuthenticationScheme = "Google",
    DisplayName = "Google",
    SignInScheme = COOKIE_AUTH,
    ClientId = "sdlfkjgsdlkfjgsdf-sdfadsfasdf.apps.googleusercontent.com",
    ClientSecret = "myClientSecretBase64==",    
});

但是,如果我这样做,我需要一种方法来链接我的谷歌登录和我的条带帐户登录。在此之前,我使用的是 IdentityServer4。通常,令牌服务器与 API 是分开的。所以托管令牌服务器似乎有点矫枉过正,如果只有一个应用程序会使用它。

是否有一种简单的方法允许身份验证,同时仍然能够连接到诸如条带之类的外部 api?

注意:如果解决方案需要 IdentityServer 4,我不介意,我宁愿不必托管 2 个单独的应用程序

【问题讨论】:

    标签: c# authentication .net-core oauth-2.0 authorization


    【解决方案1】:

    嗯,这似乎是我对身份的误解造成的。身份已经是单个用户拥有不同的登录方法。在我的身份验证控制器中,如果用户已经存在,我通过将外部登录附加到现有用户来解决此问题。

    var user = await this._userManager.FindByEmailAsync(email);
    if(null == user)
    {
        user = await this.CreateIdentityUser(info, email);
    }
    
    var addLoginResult = await _userManager.AddLoginAsync(user, info);
    

    警告:你不应该小心链接用户,如果你不顾一切地基于电子邮件链接用户,你可能会遇到另一个用户使用同一电子邮件在提供商上创建虚假帐户的问题.

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 2020-01-10
      • 2018-05-20
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      相关资源
      最近更新 更多