c# - 我需要将令牌值从我的家庭控制器传递到帐户控制器
问题描述
我在 json 中获取数据并将这些值account
从控制器传递给我的home
控制器。我的控制器中还有一个令牌值home
,我需要在我的account
控制器中。
我在下面添加代码(HomeController
代码):
public JsonResult ProfileInfo(string token)
{
var clienta = new RestClient("https://api.amazon.com/auth/o2/tokeninfo?access_token="+token);
var requestb = new RestRequest(Method.GET);
requestb.AddHeader("postman-token", "efe6939a-95b0-5ffc-f4a0-e462479e87ad");
requestb.AddHeader("cache-control", "no-cache");
IRestResponse responseb = clienta.Execute(requestb);
//code for profile
var client = new RestClient("https://api.amazon.com/user/profile");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Host", "api.amazon.com");
request.AddHeader("Postman-Token", "3c733ec4-2336-4d5d-bb82-ab0993791bc5,d4a052f7-2a98-4b2a-b057-97511ce052b7");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("User-Agent", "PostmanRuntime/7.15.2");
request.AddHeader("Authorization", "Bearer "+token);
IRestResponse response = client.Execute(request);
return Json(response+responseb.Content, JsonRequestBehavior.AllowGet);
}
下面是account
控制器代码:
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register(long? token)
{
if (Request.IsAuthenticated)
{
return RedirectToLocal("/dashboard");
}
string email = "";
string name = "";
//token;
if (Request.QueryString["email"] != null)
{
email = Request.QueryString["email"].ToString();
email = email.Substring(1);
}
if (Request.QueryString["name"] != null)
{
name = Request.QueryString["name"].ToString();
name = name.Substring(1);
}
@ViewBag.Email = email;
@ViewBag.name = name;
if (!token.HasValue)
return RedirectToAction("Register", new
{
token = DateTime.Now.Ticks
});
return View();
}
我想从我的home
控制器到account
控制器获取该令牌值。因为如果它没有从home
控制器获得令牌值,它会生成新的令牌。
解决方案
您可以作为参数集默认为空
在您的家庭控制器中使用此返回语句,这将重定向到在 ControllerName 中指定的控制器。
并在 Account 控制器中使用参数创建操作。如果您在帐户控制器的所有活动中都需要该帐户,请使用会话
以这种方式在会话中分配
Session["testData"] = testData;
接入方式
var testData= (string)Session["testData"]
return RedirectToAction("actionName", "ControllerName", new{ data: data})
public ActionResult actionName(string data = "")
{
return View();
}
推荐阅读
- java - 为什么 Sentry 不适用于我的 Log4j2
- ios - Safari 无法打开页面“localhost:8080/console”。错误是:“从 'localhost:8080/console' 取消加载,因为它使用的是 HTTP/0.9。” (:0)
- google-chrome - 无法在 Chrome 浏览器上使用谷歌“产品”
- google-bigquery - 如何检查 Google BigQuery SQL 中的多个模式?(喜欢 + 输入)
- python - 从存储在 Google Cloud Storage 中的文件创建 tar 存档
- python - 如何使用 yfinance 检测失败的下载
- c - 如何在 MacOS for Windows 上编译
- angular - Angular AsyncValidatorFn 导致 CPU 和内存使用率高
- apache - RedirectMatch 添加额外的 url 段
- php - 在本地加入 Zoom Web sdk 加入 Zoom 会议,然后遇到错误“加入会议超时”和“签名无效”