You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Home Controller中的token值传递至Account Controller?

解决方案:将Home Controller的Token传递到Account Controller

看起来你需要把Home里拿到的Amazon token传到Account的Register方法里,替代默认生成的token。这里有几个实用的方案,结合你的代码来具体说明:

方案1:后端直接跳转时传递Token

如果是从Home的某个Action直接跳转到Account/Register,直接用RedirectToAction带参数就行:

先在Home里加一个跳转的Action(或者修改现有逻辑):

public ActionResult RedirectToRegister(string token)
{
    // 这里可以先调用ProfileInfo处理你的Amazon接口逻辑
    // 然后跳转到Register并传token
    return RedirectToAction("Register", "Account", new { token = token });
}

然后修改Account的Register方法,把参数类型改成string(因为Amazon token是字符串,不是long):

[AllowAnonymous]
public ActionResult Register(string token) 
{
    if (Request.IsAuthenticated)
    {
        return RedirectToLocal("/dashboard");
    }

    // 优先用传入的token,没有的话再生成新的
    if (!string.IsNullOrEmpty(token))
    {
        ViewBag.AmazonToken = token;
    }
    else
    {
        token = DateTime.Now.Ticks.ToString();
        ViewBag.AmazonToken = token;
    }

    // 原有处理email和name的逻辑保留
    string email = "";
    string name = "";
    if (Request.QueryString["email"] != null)
    {
        email = Request.QueryString["email"].ToString().Substring(1);
    }
    if (Request.QueryString["name"] != null)
    {
        name = Request.QueryString["name"].ToString().Substring(1);
    }
    ViewBag.Email = email;
    ViewBag.name = name;

    return View();
}

方案2:前端AJAX请求后携带Token跳转

如果是前端先调用Home/ProfileInfo拿数据,再跳转到Register,那得先修改ProfileInfo返回包含token的JSON:

public JsonResult ProfileInfo(string token)
{
    // 原有调用Amazon接口的代码不变
    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);

    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);

    // 返回结构化的JSON,包含token和接口返回内容
    return Json(new {
        Token = token,
        ProfileData = response.Content,
        TokenInfo = responseb.Content
    }, JsonRequestBehavior.AllowGet);
}

然后前端拿到数据后,跳转到Register时把token拼到URL里:

// 调用Home的ProfileInfo接口
fetch(`/Home/ProfileInfo?token=${yourAmazonToken}`)
    .then(res => res.json())
    .then(data => {
        // 跳转到Account/Register并带上token
        window.location.href = `/Account/Register?token=${encodeURIComponent(data.Token)}`;
    });

这样Account的Register方法就能通过参数接收到token了。

方案3:用TempData临时存储(不暴露在URL)

如果不想把token放在URL里,用TempData存到Session中,一次有效:

在Home的Action里存token:

public ActionResult GoToRegister(string token)
{
    TempData["AmazonToken"] = token;
    return RedirectToAction("Register", "Account");
}

然后在Account的Register里读取:

[AllowAnonymous]
public ActionResult Register()
{
    if (Request.IsAuthenticated)
    {
        return RedirectToLocal("/dashboard");
    }

    // 从TempData取token,没有就生成新的
    string token = TempData["AmazonToken"] as string;
    if (string.IsNullOrEmpty(token))
    {
        token = DateTime.Now.Ticks.ToString();
    }
    ViewBag.AmazonToken = token;

    // 原有email和name处理逻辑不变
    string email = "";
    string name = "";
    if (Request.QueryString["email"] != null)
    {
        email = Request.QueryString["email"].ToString().Substring(1);
    }
    if (Request.QueryString["name"] != null)
    {
        name = Request.QueryString["name"].ToString().Substring(1);
    }
    ViewBag.Email = email;
    ViewBag.name = name;

    return View();
}

关键提醒

  • 你原来的Register方法参数是long? token,但Amazon的token是字符串,这个类型不匹配会导致参数接收失败,一定要改成string token!
  • 如果token比较敏感,优先用TempData方案,避免URL泄露;如果是公开场景,用URL参数更简单。

内容的提问来源于stack exchange,提问作者shayanitey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:48:07