简体   繁体   English

如何使用 MVC 隐藏 url 中的请求参数字段?

[英]How can i hide the Request parameter fields in url using MVC?

I am using the following to redirect and call an api: In my action method I have the following:我正在使用以下内容重定向并调用 api: 在我的操作方法中,我有以下内容:

        if (token != null)
        {            
           return Content($"https://localhost:1234/user/UserTokenLogin?token={myToken}");
        }

Now i can see the token in the url.现在我可以在 url 中看到令牌。 Is there a way I can hide this parameter field but still extract it on the destination api?有没有办法可以隐藏此参数字段但仍将其提取到目标 api 上?

You need to apply an encode/decode procedure.您需要应用编码/解码程序。 I'll give you an example with Base64 encoding but you could find more secure algorithms if you want.我会给你一个 Base64 编码的例子,但如果你愿意,你可以找到更安全的算法。

public string Encode(string strToEncode)
{
    byte[] encodedVal = System.Text.Encoding.UTF8.GetBytes(strToEncode);
    return Convert.ToBase64String(encodedVal);
}

public string Decode(string strToDecode)
{
    byte[] decodedVal = Convert.FromBase64String(strToDecode);
    return System.Text.Encoding.UTF8.GetString(decodedVal);
}

Then you can apply this as below.然后你可以如下应用它。

if (token != null)
{  
  var encodedToken = Encode(myToken);          
  return Content($"https://localhost:1234/user/UserTokenLogin?token={encodedToken}");
}

public ActionResult UserTokenLogin(string token){
     var decodedToken = Decode(token);

     //do other stuff.
}

You can use with header parameters.您可以使用 header 参数。 it is usually used in the header.它通常用于 header。

Response.Headers.Add("AuthToken", "token");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用asp隐藏来自url的查询字符串。 网络mvc - How can i hide the query string from the url using asp. Net mvc 如何将数组作为请求参数发送.Net MVC - How can I send array as a request parameter .Net MVC MVC 5 C#隐藏网址参数 - MVC 5 c# hide url parameter 我如何在asp.net MVC中从URL更改隐藏控制器名称 - how can i change-hide controller name from url in asp.net MVC 提出Ajax请求时,如何在使用c#的url参数中使用jquery变量? - When making an ajax request, how can I use a jquery variable in the url parameter that uses c#? ASP.NET MVC3:如何使用Html.DisplayForModel和Html.EditorForModel隐藏字段 - ASP.NET MVC3: How do I hide fields using Html.DisplayForModel and Html.EditorForModel 我可以使用Request.Url.Query自动删除querystring上的参数吗? - Can I automatically remove a parameter on querystring with Request.Url.Query? 为什么我不能将url中的参数直接传递给MVC中的方法(参数始终为null) - why can I not pass a parameter in the url directly to my method in MVC (the parameter is always null) MVC:如果没有值,如何隐藏表 - MVC: how can i hide a table if it has no value 我如何使用 datapost 将参数从 jqgrid 传递到 controller(使用 MVC4 和 asp.net) - how can i pass parameter from jqgrid to controller with datapost (using MVC4 and asp.net )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM