1、在nuget里添加引用
IdentityServer4.AspNetIdentity
2、下面的代码能获得accesstoken
var result = HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken).Result;
详细的引用如下:
using IServices; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Protocols.OpenIdConnect; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace DataPoolAdminCenter.Controllers { [Authorize] public class SchedulingController : Controller { IBaseInfoServices _baseInfoServces; public SchedulingController(IBaseInfoServices baseInfoServces) { _baseInfoServces = baseInfoServces; } public IActionResult Index() { //这是得到的token var token = HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken).Result; return View(); } }