Share session variables between asp.net and vb6 - asp.net

Hi i have a question about Session variables, i want to call in GET via ajax, from my .aspx page, a page .asp that have VB6 code inside, i need to share Session variables between them actually i tried this:
ASPX file
<html>
<head>
<title>let's try Ajax</title>
<script type="text/javascript" src="Scripts/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
var request = $.ajax({
url: "/Default.asp",
type: "GET",
dataType: "html",
cache: "false"
});
request.done(function(msg){
$("#insert").html(msg);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
in the aspx: <%Response.Write(Session["try"].ToString()); %>
</div>
<div id="insert">
</div>
</form>
</body>
</html>
code behind page:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["try"] = "hello world";
}
}
finally .asp page
<%
[...]
Response.Write "" & Session("try")
Response.End
[...]
%>
Actually the Session doesn't show anything in the .asp page, but in the .aspx page shows it's string inside, what's wrong with my code? Is it possible to pass share a session?

I think, you want to share your session between Class ASP and ASP.Net. so you need to store
your session information in SQL server. Please refer below link
http://msdn.microsoft.com/en-us/library/aa479313.aspx

Related

cardIdentifier returned in URL QueryString rather than hidden-field

I have integrated SagePay's 'Drop In Checkout' into a test solution. The issue is, when I am submitting the form for the 'cardIdentifier', it is returned in the URL as a QueryString, rather than a hidden field, as per the documentation.
http://integrations.sagepay.co.uk/content/getting-started-integrate-using-drop-checkout
"Once the customer initiates the form submission, the payment details are validated, tokenised and passed as a hidden field called cardIdentifier which is then posted with the rest of the form contents"
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="CustomPayment.aspx.cs" Async="true" Inherits="SagePayDropInTest.CustomPayment" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://pi-test.sagepay.com/api/v1/js/sagepay-dropin.js"></script>
</head>
<body>
<div id="main">
<h1>My Test Shop</h1>
<form id ="checkout-form">
<h2>Payment Details</h2>
<div id="payment-details"></div>
<div id="submit-container">
<input type="submit"/>
</div>
</form>
</div>
<script>
sagepayCheckout({
merchantSessionKey: '<%=MerchID%>',
containerSelector: '#payment-details'
}).form({
formSelector: '#checkout-form'
});
</script>
</body>
</html>
C# CodeBehind
namespace SagePayDropInTest
{
public partial class CustomPayment : System.Web.UI.Page
{
public string MerchID = "";
protected void Page_Load(object sender, EventArgs e)
{
MerchID = GetMerchSessionID();
}}}
It does return the cardIdentifier, but just as a QueryString, but I would rather I was getting it as a hidden-field, as documented. The rest of the integration works as documented, it is just this step which is throwing me.
I am no doubt missing something obvious, and any guidance would be much appreciated.
Try changing the <form> tag to include a method="post" attribute. This should mean that the cardIdentifier is sent as a posted field rather than in the query string. The default method for a form is normally a GET request, the SagePay JS probably doesn't change this.
Also, there looks like an extra space in the <form id ="checkout-form"> tag as it is. I would recommend taking this out as some less forgiving browsers may not parse this correctly, breaking the CSS selector in your JS.

Anti-Forgery token usage between MVC pages to classic ASP.NET pages

I have an application which has 80% of it's part in ASP.NET MVC 2.
I am using Anti-forgery token to avoid Cross-Site Request Forgery.
Say I have an action method -
public JsonResult AddMenuFavorite(int id) {
// code
}
which uses the token to prevent CSRF. I have various links in my MVC pages from there I can make a call to this action method smoothly without any error.
While making calls from classic ASP.NET pages, this shows error.
Reason:: Anti-forgery token is not embedded in ASP.NET pages it seems.
can any one help me with the solution?
In order to generate the required hidden field containing the token you could use the AntiForgery.GetHtml static method:
<%# Page Language="C#" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= AntiForgery.GetHtml() %>
<asp:LinkButton
runat="server"
ID="btn"
PostBackUrl="~/SomeController/AddMenuFavorite/123"
Text="Go to the MVC site"
/>
</div>
</form>
</body>
</html>
And since your action returns JSON I suspect that you are calling it using an AJAX request. In this case you can use the value of the hidden field generated by the helper to send it along with the AJAX request:
$(function() {
$('#someLink').click(function() {
$.post(this.href, $('form').serialize(), function(result) {
// do something with the result
});
return false;
});
});

ResolveUrl not working inline

I am getting the error on the below code in asp.net 4.0
<script type="text/javascript" src='<%#=ResolveUrl("~/Scripts/jquery-1.4.1.js")%>'></script>
Error Message: CS1525: Invalid expression term '='
I am using this code in Site.Master in head tag
You can't use <%# and <%= at the same time. Try it like this:
<script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery-1.4.1.js")%>'></script>
EDIT
If you are getting an error that states:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
when you try to use <%= ResolveUrl(..., it is because something in your code is attempting to add controls to your header control in Site.Master. If that is the case, switch the script tag to read:
<script type="text/javascript" src='<%# ResolveUrl("~/Scripts/jquery-1.4.1.js")%>'></script>
and make sure you call the DataBind() method on the header tag at some point (for example, from the Page_Load method for Site.Master):
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}
}
You can use ResolveUrl with Eval like this. No external code needed.
<img src='<%# ResolveUrl(Eval("FILE_URL").ToString()) %>' alt=""
style="width:50px;height:50px"/>

Disable/deny going back to previous page on asp.net 2.0

On my current project I want to deny going back once my customers hit submit button and redirected to the final page. How can I deny from going back to previous page or some mechanism to expire the page if the back button is clicked. I tried this code and it didn't work
page.response.cache.setcacheability(httpcacheability.nocache)
<script type="text/javascript">
history.forward();
</script>
</head>
<body>
<form runat="server" id="form1">
<%
Response.Buffer = True
Response.Expires = 0
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1)
Response.CacheControl = "no-cache"
%>
I put this in my master page and it works great!! :-)
Place the following between the <head> </head> of ALL your pages:
<script type="text/javascript">
history.forward();
</script>
And check this article out:
https://web.archive.org/web/20210927201700/http://www.4guysfromrolla.com/webtech/111500-1.shtml
I also had the same problem, use this Java script function, its 100% working fine, would not let you go back.
<script type = "text/javascript" >
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
</script>

ASP.NET pass a javascript value in server side

Hi how can I pass a client side (JS) value in server side (C#)?
e.g.
I have a generated table (after uploading images) and it contains images and I want to select the image and throw the ID back in server side.
The uploade I used was JQuery Uploadify and I have a "onComplete" function
(simple code)
'onComplete': function (event, queueID, fileObj, response, data) {
$('#imgs').append('<img id="' + queueID + '" src="' + response + '" alt="' + response + '" />');
How can I do this?
To send a value from javascript to the server you have a couple of options:
Use AJAX if you want to stay on the current page
Redirect to a server side script and pass the value in the querystring
Let's consider the first case:
Assuming you have a web method capable of receiving the value on the server:
<%# Page Title="Home Page" Language="C#" AutoEventWireup="true" %>
<script type="text/C#" runat="server">
// Server side script in the code behind that will receive
// the value: The method needs to be static
// and decorated with the WebMethod attribute
[System.Web.Services.WebMethod]
public static string Foo(string id)
{
return "ok";
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
// Send an AJAX request to the server
$.ajax({
url: '/default.aspx/foo',
type: 'POST',
contentType: 'application/json; charset=utf-8',
// Pass the value as JSON string
// You might need to include json2.js for the JSON.stringify
// method: http://www.json.org/json2.js
data: JSON.stringify({ id: 'someId123' }),
success: function (result) {
// The result is also JSON
alert(result.d);
}
});
});
</script>
</head>
<body>
<form id="Form1" runat="server">
</form>
</body>
</html>
are you using Asp.net Forms? If so, you do understand the concept of Page Lifecycle and how it affects the state of the page?
I would suggest going javascript all the way in this one, please see jQuery + AJAX.

Resources