Xero API 身份验证 - 使用 C#、ASP.NET Core MVC 构建身份验证应用程序

问题描述 投票:0回答:1

我正在尝试开发将集成到 Xero API 中的软件,但现在我首先需要构建一个身份验证应用程序。我不确定从哪里开始,因为我对此很陌生,而且对处理一般 API 也很陌生。

在 Github 和 Xero 开发者门户上,他们提供了有价值的信息并提供了很多帮助,唯一的问题是我不知道如何将所有内容组合在一起,因此缺乏经验。有人可以指导我如何以及从哪里开始吗?

https://github.com/XeroAPI/Xero-NetStandard?tab=readme-ov-file#configuration

这是一个链接,其中包含他们为身份验证提供的示例代码,还有更多内容。

我希望使用 C#、ASP.NET Core MVC 连接应用程序以使用 Xero 登录并通过 Xero 进行身份验证。

这是他们提供的身份验证流程示例。我只需要关于从哪里开始等等的指导。

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Xero.NetStandard.OAuth2.Client;
using Xero.NetStandard.OAuth2.Config;
using Xero.NetStandard.OAuth2.Token;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Xero.NetStandard.OAuth2.Models;
using System.Collections.Generic;

namespace XeroNetStandardApp.Controllers
{
    public class XeroOauth2Controller : Controller
    { 
        private readonly ILogger<HomeController> _logger;
        private readonly IOptions<XeroConfiguration> XeroConfig;

        public XeroOauth2Controller(IOptions<XeroConfiguration> config, ILogger<HomeController> logger)
        {
            _logger = logger;
            this.XeroConfig = config;
        }

        public IActionResult Index()
        {
            XeroConfiguration xconfig = new XeroConfiguration();
            xconfig.ClientId = "yourClientId";
            xconfig.ClientSecret = "yourClientSecret";
            xconfig.CallbackUri = new Uri("https://localhost:5001");  // default for standard webapi template
            xconfig.Scope = "openid profile email offline_access files accounting.transactions accounting.contacts";

            var client = new XeroClient(xconfig);

            return Redirect(client.BuildLoginUri());
        }
    }
}

上面代码的第2步:

XeroConfiguration xconfig = new XeroConfiguration(); 

xconfig.ClientId = "yourClientId";
xconfig.ClientSecret = "yourClientSecret";
xconfig.CallbackUri = new Uri("https://localhost:5001") //default for standard webapi template
xconfig.Scope = "openid profile email files accounting.transactions accounting.contacts offline_access";

var client = new XeroClient(xconfig);

// before getting the access token please check that the state matches
await client.RequestAccessTokenAsync(code);

// from here you will need to access your Xero Tenants
List<Tenant> tenants = await client.GetConnections();

// you will now have the tenant id and access token
foreach (Tenant tenant in tenants)
{
    // do something with your tenant and access token
    // client.AccessToken;
    // tenant.TenantId;
}

配置:

XeroConfiguration xconfig = new XeroConfiguration();
xconfig.ClientId = "yourClientId";
xconfig.ClientSecret = "yourClientSecret";
xconfig.CallbackUri = new Uri("https://localhost:5001"); //default for standard webapi template
xconfig.Scope = "openid profile email offline_access files accounting.transactions accounting.contacts";

var client = new XeroClient(xconfig);
{
  "id_token": "xxx.yyy.zz",
  "access_token": "xxx.yyy.zzz",
  "expires_in": 1800,
  "token_type": "Bearer",
  "refresh_token": "xxxxxxxxx",
  "scope": "email profile openid accounting.transactions offline_access"
}
xeroToken.AccessToken
xeroToken.RefreshToken
xeroToken.IdToken
xeroToken.TokenType
xeroToken.ExpiresAtUtc
c# oauth-2.0 asp.net-core-mvc xero-api
1个回答
0
投票

您共享的链接已经包含其工作原理。

如果你想从Xero获取访问令牌,你应该首先在这个控制器中创建一个测试XeroOauth2Controller,你可以创建一个登录重定向方法和一个回调方法。

回调方法内部,包含存储token的方法。然后您可以使用存储的访问令牌来访问 Xero API。

更多详情,可以参考以下代码:

1.在项目中安装这两个包:

Install-Package Xero.NetStandard.OAuth2
Install-Package Xero.NetStandard.OAuth2Client
  1. 创建控制器以使用 Xero 进行身份验证并获取代码以获取访问令牌。

代码如下:

public class XeroOauth2Controller : Controller
{
    private readonly ILogger<HomeController> _logger;
  
    public XeroOauth2Controller( ILogger<HomeController> logger)
    {
        _logger = logger;
 
    }

    public IActionResult Index()
    {
        XeroConfiguration xconfig = new XeroConfiguration();
        xconfig.ClientId = "yourClientId";
        xconfig.ClientSecret = "yourClientSecret";
        xconfig.CallbackUri = new Uri("https://localhost:5001"); //default for standard webapi template
        xconfig.Scope = "openid profile email offline_access files accounting.transactions accounting.contacts";

        var client = new XeroClient(xconfig);

        return Redirect(client.BuildLoginUri());
    }
   

    /// <summary>
    /// Callback for authorization
    /// </summary>
    /// <param name="code">Returned code</param>
    /// <param name="state">Returned state</param>
    /// <returns>Redirect to organisations page</returns>
    public async Task<IActionResult> Callback(string code, string state)
    {
        XeroConfiguration xconfig = new XeroConfiguration();
        xconfig.ClientId = "yourClientId";
        xconfig.ClientSecret = "yourClientSecret";
        xconfig.CallbackUri = new Uri("https://localhost:5001"); //default for standard webapi template
        xconfig.Scope = "openid profile email offline_access files accounting.transactions accounting.contacts";

        var client = new XeroClient(xconfig);
        //here we could get the accesstoken 
        var xeroToken = (XeroOAuth2Token)await client.RequestAccessTokenAsync(code);

      
        // store the access token , you could modify it to store the token inside the Memroy Cache or else based on your requirement
        Response.Cookies.Append("X-Access-Token", xeroToken.AccessToken);

        // inside the redirect method you could use the API client to the get the info based on the cookie's accesstoken
        return RedirectToAction("Index", "Home");
    }
 
}

更多详情,您可以参考其标准样本

© www.soinside.com 2019 - 2024. All rights reserved.