我需要将令牌值从家庭控制器传递到帐户控制器

时间:2019-07-30 11:57:13

标签: c# asp.net-mvc

我正在获取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控制器获取令牌值,则会生成新令牌。

1 个答案:

答案 0 :(得分:1)

您可以将默认设置为空

作为参数集传递

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

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

以这种方式进行会话分配

     Session["testData"] = testData;

访问方式

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



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

   public ActionResult actionName(string data = "")
   {
     return View();
   }
相关问题