I have One conroller method like
public ViewResult MyMethod(long id, string pId)
{
}
I have one query string like
?'id=' + 10 + '&pId=' + 15
I want to encrypt it using some encryption algorithm after that i got query sting in some format like
gaiSXZyTAq6Z0a5TzsrdG2LjIj0moe2m4D0qQiG7zuQ=
I am decrypting it from Global.asax in begin Request, Able to decrypt and setting Query string All Keys but controller not able to get its parameter value
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString)))
{
var newQueryString = SecurityEncryption.DecryptionValue(HttpUtility.UrlDecode(Convert.ToString(Request.QueryString)).Replace(" ", "+"));
Request.QueryString.AllKeys[0] = newQueryString;
}
}
I want that Controller Method will get its Parameter values,How can I achieve this?
Please any one can help me.
I found sollution I decrypt my base controller
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var queryStringQ = Server.UrlDecode(filterContext.HttpContext.Request.QueryString["q"]);
if (!string.IsNullOrEmpty(queryStringQ))
{
// Decrypt query string value
var queryParams = DecryptionMethod(queryStringQ);
}
}
Related
I would like to "intercept"/alter the OData query that's generated when using OData with the Web API.. but I'm not entirely sure of how to "extract" the generated query.. I assume that the OData filter,expands and more some how gets generated to some sort of expression tree or some sort of query.. and if that's the case, then that's the type of query I would like to be able to alter before its sent to the database as an SQL-command.
I have searched the net for some way of extracting the generated expression tree.. but hasn't be able to find sufficient information, so I was sort of hoping that someone here has some more insight of how the whole OData-"framework" works..
Any ideas of where to start?
You can alter Odata url before it is executed. Inherit from EnableQueryAttribute class and change url. Following is a real case to change guid string format to hexadecimal string format before it's sent to oracle.
public class EnableQueryForGuid : EnableQueryAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var url = actionContext.Request.RequestUri.OriginalString;
var newUrl = ModifyUrl(url);
actionContext.Request.RequestUri = new Uri(newUrl);
base.OnActionExecuting(actionContext);
}
private string ModifyUrl(string url)
{
Regex regex = new Regex(#"%27([A-Za-z0-9]{32})%27");
var res = regex.Matches(url);
if (res.Count > 0)
{
var guidPart = res[0].Value.Remove(0, 3);
guidPart = guidPart.Remove(guidPart.Length - 3, 3);
var guidValue = new Guid(BitConverter.ToString((new Guid(guidPart)).ToByteArray()).Replace("-", ""));
url = url.Replace(res[0].Value, guidValue.ToString());
}
return url;
}
}
then use this new attribute on your controller method:
[HttpGet]
[EnableQueryForGuid]
[ODataRoute("GetSomething")]
public IHttpActionResult GetSomething()
{
....
}
original query:
OData/GetSomething?&$filter=MyGuid%20eq%20%272C3C7BC0EC7FA248B0DEE3DAA371EE73%27
altered query:
OData/GetSomething?&$filter=MyGuid%20eq%20e5794d6a-5db1-475a-8c49-0f91a8f53c8a
I'm creating an operating system in COSMOS when I was running into this little problem.
else if (HOLOOS.seperate(MyGlobals.input, 5) == "login")
{
if (MyGlobals.input == "login")
{
Console.Write(Commands.login.usage);
}
else
{
var arg = HOLOOS.rseperate(MyGlobals.input, 6, (MyGlobals.input.Length - 1));
arg = HOLOOS.GatherArgs(arg);
login.run(arg);
}
}
This is the login class.. I guess something is wrong with the public static void run?
class login
{
public static string CurrentUser;
public static void run(string EnteredUser, string EnteredPassword, string User1CorrectName, string User1CorrectCode, string User2CorrectName = "", string User2CorrectCode = "")
{
string EnteredHashedPassword = BashUtils.Encrypt(EnteredPassword);
//Check if the user name is
if (EnteredUser == User1CorrectName)
{
//If the user name entered is jacob, then check if the password is OK
if (EnteredHashedPassword == BashUtils.Encrypt(User1CorrectCode))
{
//If password is okay than login
Console.Write("You have sucessfully logged in as " + User1CorrectName);
CurrentUser = User1CorrectName;
cd.Path = "D:\\" + User1CorrectName + "\\";
}
//If the password is not OK then say so
else
{
Console.Write("Not correct password for " + User2CorrectName);
}
}
You are passing one argument in this line:
login.run(arg);
to the method run()
when the signature of the method is this:
public static void run(string EnteredUser, string EnteredPassword, string User1CorrectName, string User1CorrectCode, string User2CorrectName = "", string User2CorrectCode = "")
As you can see, the first 4 parameters are mandatory, so you should pass them to the function. Or modified run's signature.
The last 2 parameters have a default value, the empty string "". So, you could not pass those values if you don't need them (this will be assigned with the default value if you don't pass it as an argument).
Read this documentation for parameters and default values MSDN for a complete description with many examples.
I would definitely go with named parameters in that case, but this is just an opinion. Read the docs, if you don't understand something just ask.
In my app I am building my own media player. When a user selects a song to play want to be able to pass the link to the sample media and the metadata associated with it (Artist, Track, Album Art, etc.) The part I'm stuck on is how to group all the data and pass it to the media player page. Here's what i have so far.
Determine what item was selected and add data to query string.
UPDATED
public void musicSampleSelectedHandler(object sender, RoutedEventArgs e)
{
Track selected = (sender as Image).DataContext as Track;
ListBoxItem pressedItem = this.listReleaseMain.ItemContainerGenerator.ContainerFromItem(selected) as ListBoxItem;
if (pressedItem != null)
{
string _rT = selected.title;
string _rN = selected.release.name;
//string _rA = selected.artists; ????
string _rI = selected.images.large.url;
string _rS = selected.sampleUrl;
this.NavigationService.Navigate(new Uri("/Pages/MediaPage.xaml?releaseName=" + _rN + "&releaseTrack=" + _rT + "&releaseImage=" + _rI
+ "&releaseSample=" + _rS, UriKind.Relative));
}
}
OnNavigatedTo method to pull data out of the query string
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string releaseName = String.Empty;
string releaseImg = String.Empty;
string releaseUrl = String.Empty;
if (NavigationContext.QueryString.TryGetValue("releaseUrl", out releaseUrl))
{
sampleMedia.Source = new Uri(releaseUrl, UriKind.Absolute);
}
}
I'm not sure if I can use the query sting to pass multiple pieces to the media player, or if I to do something different to pass the data to the other page. All of my data comes from the web using a webclient. Thanks for the help.
QueryString is just a Dictionary of the parameters passed in via the Uri. The Uri uses the standard syntax of passing in parameters delimited by &. So in your example, if you had something like:
this.NavigationService.Navigate(new Uri("/Pages/MediaPage.xaml?releaseUrl=" + releaseUrl + "&releaseImg=" + releaseImg , UriKind.Relative));
then you can then parse this out using something like:
if (NavigationContext.QueryString.TryGetValue("releaseUrl", out releaseUrl))
{
sampleMedia.Source = new Uri(releaseUrl, UriKind.Absolute);
}
if (NavigationContext.QueryString.TryGetValue("releaseImg", out releaseImg))
{
// do something with releaseImg
}
I am using Querystring to pass values from one page to other. I am tring to implement encoding and decoding using the Server.UrlDecode and urlEncode.
Query string returns a null value, but I can check the values are been sent in URL.
The two pages are:
QueryString.aspx
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string id = "1";
string name = "aaaa";
string url = string.Format("QueryStringValuesTransfer.aspx?{0}&{1}", Server.UrlEncode(id), Server.UrlEncode(name));
Response.Redirect(url);
}
;;
In another page :
QueryStringValuesTransfer.aspx:
public partial class QueryStringValuesTransfer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id1 = Server.UrlDecode(Request.QueryString["id"]);
string name1 = Server.UrlDecode(Request.QueryString["name"]);
Response.Write(id1 + name1);
}
}
I am get null values in the id1 and name1.
Any help please..
Change this line:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
Right now you are only setting the values in the querystring, you need to assign them names so you can grab them again:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
That's because your query string should be something like
MyPage.aspx?id=xxx&name=yyy
You are not passing the values, only the names...
string url = string.Format("QueryStringValuesTransfer.aspx?{0}&{1}", Server.UrlEncode(id), Server.UrlEncode(name));
Should be:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
You aren't specifying a name for the values. You need:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
When constructing the URL in the first page you should do this:
string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
The query string consists of key-value pairs, you should provide the keys.
I´m trying to use session to store a value (id). The problem is that I have to store it as a string. When trying to use the instance of the id I get the error:
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 156:
Line 157: Nemanet_Navigation newFile = new Nemanet_Navigation();
Line 158: newFile.Nav_pID = (Guid)Session["id"];
Line 159:
Line 160:
This is where I get the id and that seems to work fine. Session["id"] gets the value.
public void treeview_Navigation_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode node = treeview_Navigation.FindNode(treeview_Navigation.SelectedNode.ValuePath);
NavTreeNode nNode = node as NavTreeNode;
Session["id"]=((TreeView)sender).SelectedValue.ToString();
}
But this code does not seem to work. I get the error mentioned above.
protected void Button1_Click(object sender, EventArgs e)
{
Nemanet_Navigation newFile = new Nemanet_Navigation();
newFile.Nav_pID = (Guid)Session["id"];
}
Use Guid.Parse
protected void Button1_Click(object sender, EventArgs e)
{
Nemanet_Navigation newFile = new Nemanet_Navigation();
newFile.Nav_pID = Guid.Parse(Session["id"] as string);
}
Try using:
newFile.Nav_pID = new Guid(Session["id"].ToString());
You are converting GUID to string before saving it in session collection. But when you are retrieving it, you are trying to cast it in GUID which is invalid. String and GUID are not compatible types. Either save it as GUID or convert it into string (like you are doing) when saving and use GUID constructor that takes string to reform GUID instance.
You can do it like this:
Session["id"]=((TreeView)sender).SelectedValue.ToString();
and then retrieve it from session like:
newFile.Nav_pID = new Guid((string)Session["id"]);
By default the SessionID created by either aspx is not a GUID. You can created your own data type session id value. e.g :
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddMinutes(1.0);
myCookie.Value = g1.ToString();