首页 > 解决方案 > 我需要将令牌值从我的家庭控制器传递到帐户控制器

问题描述

我在 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控制器获得令牌值,它会生成新的令牌。

标签: c#asp.net-mvc

解决方案


您可以作为参数集默认为空

在您的家庭控制器中使用此返回语句,这将重定向到在 ControllerName 中指定的控制器。

并在 Account 控制器中使用参数创建操作。如果您在帐户控制器的所有活动中都需要该帐户,请使用会话

以这种方式在会话中分配

     Session["testData"] = testData;

接入方式

   var testData= (string)Session["testData"]



   return RedirectToAction("actionName", "ControllerName", new{ data: data})

   public ActionResult actionName(string data = "")
   {
     return View();
   }

推荐阅读