DotNetOpenAuth Oauth 2,代码响应怎么办?

DotNetOpenAuth Oauth 2, what to do with code response?

我正在为我们的 Web 应用程序中的用户授权 LinkedIn 应用程序。在基础上,它在 .NET 4.5.2 上使用 angularjs 和 .NET web api 后端。

我确实让它工作,直到回调。这是代码:

public string GetAuthenticationUrl(string context, string redirectUrl)
{
        if(context == null)
            throw new ArgumentException(nameof(context));
        if (string.IsNullOrEmpty(redirectUrl))
            throw new ArgumentException(nameof(redirectUrl));

        var appContext = _applicationContextProvider.GetLinkedInApplicationContext();
        var linkedIn = new UserAgentClient(new AuthorizationServerDescription
        {
            TokenEndpoint = new Uri(appContext.AccessTokenEndpoint),
            AuthorizationEndpoint = new Uri(appContext.AuthorizationEndpoint),
            ProtocolVersion = ProtocolVersion.V20
        }, appContext.ClientId, appContext.ClientSecret);

        var target = linkedIn.RequestUserAuthorization(null, context, new Uri(redirectUrl));

        return target.AbsoluteUri;
    }

我可以用哪个 returns a Url 去 LinkedIn。 我授权该应用程序并成功重定向到我的重定向Url,在那里我得到一个代码和一个状态。

但是接下来我该怎么做才能请求访问令牌?

public void Confirm(string state, string code)
{
// ?
}

我使用 dotnetopenauth 4.3

最佳反应 乔纳斯

我明白了。所以对于未来的冒险者:

public void Confirm(string context, string code)
    {
        if (context == null)
            throw new ArgumentException(nameof(context));
        if (string.IsNullOrEmpty(code))
            throw new ArgumentException(nameof(code));

        var appContext = _applicationContextProvider.GetLinkedInApplicationContext();
        var linkedIn = new UserAgentClient(new AuthorizationServerDescription
        {
            TokenEndpoint = new Uri(appContext.AccessTokenEndpoint),
            AuthorizationEndpoint = new Uri(appContext.AuthorizationEndpoint),
            ProtocolVersion = ProtocolVersion.V20
        }, appContext.ClientId, appContext.ClientSecret)
        {
            ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(appContext.ClientSecret)
        };

        var auth = linkedIn.ProcessUserAuthorization(new Uri($"http://fake.url?code={code}"), new AuthorizationState
        {
            Callback = new Uri("<addd original return_url here from code request>")
        });
    }