gRPC 服务中的 Request.Cookie[ "refreshToken" ] 中的问题



我试图通过请求获得刷新令牌,但它给了我一个错误CS0117说请求不存在。我不知道是否因为我使用gRPC服务或其他东西,因为我在Web Api中使用它,它工作完美。

这是我使用的代码行和库

图书馆

using Grpc.Core;
using Grpc_Server_1.Context.UserDAL;
using Grpc_Server_1.Entities;
using Grpc_Server_1.Entities.Interfaces;
using Grpc_Server_1.Protos;
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Web;

出现错误的行代码

//This is just to get the refresh token of a user, check if is empty or not
if (userInDb.RefreshToken.IsNullOrEmpty() == false){

//This is my error, gives me error in Request, I also try the line below but it don't 
//work

var a = Request.Cookies["refreshToken"]; 
//var a = HttpContext.Current.Request.Cookies["refreshToken"];
//check if user refresh token equals refresh token from a cookie           
if (!userInDb.RefreshToken.Equals(a))
{ //throw a Exception error
}
}

这是问题(如何获得请求/HttpRequest),我也使用。net 6为我的项目,所以问题可以在程序的某些部分。cs或也在appSettings,甚至也在一些NuGet包。欢迎回答。

编辑/半回答:

对于没有得到错误,我使用HttpContext作为一个变量,像这样:

private readonly HttpContext _httpContext;
//...
var a = _httpContext.Request.Cookies["refreshToken"];

现在的问题是,"这行吗?"因为我不知道这个实现是否是修复错误的最佳方法,也不知道gRPC服务是否能识别这个

无论如何,我将把这个答案,如果谁将有未来的问题

最新更新