【问题标题】:.Net-Core built in Authorization Code Flow.Net-Core 内置授权代码流
【发布时间】:2019-06-15 10:43:48
【问题描述】:

我正在努力简化我的应用程序的登录流程。抢先使用 IdentityServer 进行登录,但我不需要整个令牌服务器,所以我现在正在降级以仅使用 Asp.Net Identity。

以前我可以通过这样做通过第三方登录:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
    // Request a redirect to the external login provider.
    var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "Account", new { returnUrl });
    var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
    return Challenge(properties, provider);
}

这让我可以通过 (Coinbase) 等第三方登录,但我对它的工作原理感到困惑,因为我看不到他们接收授权码的地方。

我已经从我的 oauth 提供者那里获得了一个授权码,现在我需要获得访问令牌。我可以通过手动发出请求轻松做到这一点,例如

POST /oauth/token HTTP/1.1
Host: authorization-server.com

grant_type=authorization_code
&code=xxxxxxxxxxx
&redirect_uri=https://example-app.com/redirect
&client_id=xxxxxxxxxx
&client_secret=xxxxxxxxxx

但我觉得 .net 中有一些内置功能可以执行此请求并将令牌存储在用户管理器中。有谁知道这样做的内置方法?

【问题讨论】:

    标签: c# oauth .net-core authorization


    【解决方案1】:

    我建议坚持使用 Identity Server Nuget 包进行客户端身份验证,即使您不需要服务器。

    它提供了您在后台引用的功能来处理各种 OATH 流,因此您不必自己实现它们。

    否则,您将不得不在您的客户端、服务器端应用程序和您正在验证的服务之间手动请求。这可以根据您的需要获得quite complex

    使用 Nuget 包,您可以使用 OpenID Connect 连接外部身份验证提供程序,无论是 Coinbase 还是其他一些服务,只需通过依赖注入的几行代码。

    从那里你可以handle the callback and sign the user in

    有关详细信息,请查看官方 Identity Server 文档中的 Sign-in with External Identity ProvidersAdding Support for External Authentication 页面。

    默认情况下,Asp.Net 身份将在本地端点“/account/ExternalLogin”下处理回调。如果您需要自定义功能,请从基本 RCL 中 you can scaffold that page 并对其进行自定义。

    【讨论】:

    • 是的,我本来想这样做,但是所有示例都使用 (ASP.NET) MVC 进行外部登录,我想使用 Angular,但我没有看到一个接受IdentityServer 中的授权码。
    • 文档中有纯 java 脚本客户端示例应用程序,因此在 Angular 中实现它应该是小菜一碟。
    【解决方案2】:

    原来我想太多了。我可以使用在 Identity Server 中配置的相同 OAuth 提供程序并将它们移植到我的 .Net-Core 项目,然后我可以使用相同的登录方法

    [HttpPost]
    [AllowAnonymous]
    public IActionResult ExternalLogin(string provider, string returnUrl = null)
    {
        // Request a redirect to the external login provider.
        var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "Account", new { returnUrl });
        var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
        return Challenge(properties, provider);
    }
    

    要挑战我在 Angular 中唯一需要做的事情就是提供一个表单来调用我的外部登录方法:

    <form #form method="post" class="form-horizontal" action="https://localhost:44370/Account/ExternalLogin">
        <div>
            <p>
                <button ion-button block [disabled]="isDisabled" (click)="form.submit()" type="submit" title="Log in using your Coinbase account">
                    Coinbase
                </button>
                <input type="hidden" name="provider" value="Coinbase">
            </p>
        </div>
    </form>
    

    【讨论】:

    • 没错!这就是我回答的要点。
    • @AlexanderHiggins 这都是 .Net-Core 内置的,不需要身份框架,如果你愿意,编辑你的答案并在我的答案中重新发布代码,我会删除我的答案并标记你的正确
    猜你喜欢
    • 2021-04-29
    • 2021-05-17
    • 1970-01-01
    • 2018-09-22
    • 2014-11-04
    • 2017-04-20
    • 2016-10-15
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多