Transfer ASP.NET session to other ASP.NET solution - c#

I got a 2 ASP.NET solution. The first one is called HomePage and the other Main. I'm using IIS 7.5. At the root of IIS, I got 2 folders with the same name (HomePage and Main). Each solution is in their own folder.
I'm trying to transfer a session from HomePage to Main.
Main project (file ASPNETToASPNET.aspx)
private void Page_Load(object sender, System.EventArgs e)
{
string queryString = String.Empty;
string destPage = Request.Form["destpage"].ToString();
...
}
HomePage.aspx
private void Redirect_Click(object sender, CommandEventArgs e)
{
Response.Redirect("http://www.website.com/Main/Pages/ASPNETToASPNET.aspx?destpage=" + e.CommandArgument + "&SessionNoClient=" + Session["SessionNoClient"], false);
Response.Redirect("./Main/Pages/ASPNETToASPNET.aspx?destpage=" + e.CommandArgument + "&SessionNoClient=" + Session["SessionNoClient"], false);
}
When using the first redirect, I reach the appropriate file, but I got an error at line
string destPage = Request.Form["destpage"].ToString();
Object reference not set to an instance of an object.
When using the other redirect, I'm not able to find a way to reach the appropriate file.
As mentioned, both solutions are on same server and use the same domain.
Any solutions?

string destPage = Request.QueryString["destpage"];
This is not a transfer of session, you're just passing query parameters.
Request.Form collection contains POST values. For query string parameters (GET) use Request.QueryString or Request.Params, the last one searches your value in QueryString, Form, Cookies, and ServerVariables.

Related

Grabbing Dropbox access token on Windows Form using Dropbox API

I have done a class which already works with the Dropbox API uploading files, downloading, deleting and so on. It has been working quite well since I was just using my own access token, but I need to register other users and a single but "big" problem appeared: retrieving the access token.
1.- Redirect URI? I'm starting to doubt why do I need this. I finally used this URI (https://www.dropbox.com/1/oauth2/redirect_receiver) because "The redirect URI you use doesn't really matter" Of course I included this one on my app config on Dropbox.
2.- I reach the user's account (I can see the user's count increased and I see the app has access to the user's account.
3.- I have a breakpoint on my code to inspect the variables in order to apply the DropboxOAuth2Helper.ParseTokenFragment but I have no success on there.
This is my code, but on the if before the try catch is where it gets stuck:
string AccessToken;
const string AppKey = "theOneAtmyAppConfigOnDropbox";
const string redirectUrl = "https://www.dropbox.com/1/oauth2/redirect_receiver";
string oauthUrl =
$#"https://www.dropbox.com/1/oauth2/authorize?response_type=token&redirect_uri={redirectUrl}&client_id={AppKey}";
private string oauth2State;
private bool Result;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Start(AppKey, webBrowser1);
webBrowser1.Navigating += Browser_Navigating;
}
private void Start(string appKey, WebBrowser w)
{
this.oauth2State = Guid.NewGuid().ToString("N");
Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OauthResponseType.Token, appKey, redirectUrl, state: oauth2State);
w.Navigate(authorizeUri);
}
private void Browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (!e.Url.ToString().StartsWith(redirectUrl, StringComparison.InvariantCultureIgnoreCase))
{
// we need to ignore all navigation that isn't to the redirect uri.
return;
}
try
{
OAuth2Response result = DropboxOAuth2Helper.ParseTokenFragment(e.Url);
if (result.State != this.oauth2State)
{
// The state in the response doesn't match the state in the request.
return;
}
this.AccessToken = result.AccessToken;
this.Result = true;
}
catch (ArgumentException)
{
// There was an error in the URI passed to ParseTokenFragment
}
finally
{
e.Cancel = true;
this.Close();
}
}
I've been fighting against this for hours and I'm starting to see the things a little cloudy at this point.
This is the tutorial I used, but I'm not moving forward. I would really appreciate any help!
EDIT: I finally made some steps forward. I changed the line which contains
Uri authorizeUri2 = DropboxOAuth2Helper.GetAuthorizeUri(appKey);
Now I'm showing the generated access token on the WebClient! Bad part comes when trying to get it (it gets inside the if) and it gets generated every time I ask the user for permission, so it gets overwrited.
EDIT 2: I noted the token I get generated on the browser is somehow malformed. I try to manually change it hardcored when I'm debugging and I get an exception when an AuthException when creating the DropboxClient object :( What the hell!
As Greg stated, the solution was using the event Browser_Navigated. Looks like the version of the embedded IE my Visual Studio (2015) uses didn't notice that if it's a redirect, it won't launch the event BrowserNavigating.

Crystal Report Database LogOn when transferred to Server

how can i solve this?
when i run my system through my local machine, it does not need any Log On to Database authentication, i can see my reports directly. but when i transferred my system into the server so the others can access it remotly, it requires a logon database and even the information is correct.
here my .cs code
public partial class PrintPreview : System.Web.UI.Page
{
ReportDocument crPrint = new ReportDocument();
protected void Page_Init(object sender, EventArgs e)
{
if (Session["ID"] == null)
{
Response.Redirect("../Account/LogIn.aspx");
}
else
{
string i = Session["tID"].ToString();
//CrystalReportViewer1.Visible = true;
string reportPath = Server.MapPath("../CrystalReport2.rpt");
crPrint.Load(reportPath);
crPrint.SetDatabaseLogon("sa", "Pr0cess", "172.20.3.24", "PO");
crPrint.SetParameterValue("MOSEFNo", i);
CrystalReportViewer1.ReportSource = crPrint;
}
}
}
the page always need the database log on when i transfer it to the database, how come?
and when i transfer the folder to the IIS, the "asp_client" folder is always empty, how do i fix this? thanks!
The problem is that when you reload the page the Session["ID"] will be reset to NULL. Try to check your Session State to state server mode.

How can I add a text ("https://") to a string that does not start with https:// but instead www?

I recieve data and my label (named website) has both urls that start with and without https, so when a label starts with www and not the http I wish to add that to the string so that the link will work to those who hasnt put https in their url-link.
This is what I currently have:
URLButton.Clicked += (object sender, EventArgs e) => {
Uri outvalue;
if(Uri.TryCreate(website.Text, UriKind.Absolute, out outvalue))
{
Device.OpenUri(outvalue);
}
};
With this current code the URL's that does not have http but www instead in the beginning will not be clickable.
So if a website URL does not have the https (like some of them dont) I need to add the https manually in the code but I am not sure how I could do it.
If (website.text != (start with http))
"https://" + website.text
else
website.text = website.text or something like that
A more elegant solution would be to use the UriBuilder class (which can also add a missing www.):
URLButton.Clicked += (object sender, EventArgs e) => {
Uri value = new UriBuilder(website.Text).Uri;
website.Text = value.AbsoluteUri;
Device.OpenUri(value);
}
Brute force:
if (!website.Text.StartsWith("https://") ) {
website.Text= "https://" + website.Text;
}
Would probably want more robust validation depending on possible input permutations.
To be clear, what you want to do is convert something like www.google.se to http:\\www.google.se?
if(!website.Text.StartsWith("http://"))
{
website.Text ="http://" + website.Text;
}

Is the any Way to Make a Variable into a file address in C#

I tried to make a simple app just for giving link to it and it download the direct link through an address and I have to put the name file with format in the directory address so I want something that make it changeable by the user. Any suggestions?
private void button1_Click(object sender, EventArgs e)
{
string Link;
Link = textBox1.Text;
if (!Directory.Exists(#"C:\Downloaded_Data"))
{
Directory.CreateDirectory(#"C:\Downloaded_Data");
}
else
{
}
string Dir;
Dir = #"C:\Downloaded_Data\ + txtName + txtFormat";
WebClient wc = new WebClient();
wc.DownloadFile(Link, Dir);
SoundPlayer Play = new SoundPlayer(#"E:\SteamLibrary\SteamApps\common\GarrysMod\garrysmod\sound\thrusters\jet01.wav");
Play.Play();
}
So I want that txtName and txtFormat can be changed in the textboxes you know I want them to add to the directory but I don't know how! Maybe becasue I just started :)
See http://msdn.microsoft.com/en-us/library/system.io.packaging.package.aspx There is an API which allows a Stream to be created in such a way that it may have a URI specified which points directly to said stream.
So in short if you have an array or a list and you want to provide a URI to point to the data which can be consumed by a 'Stream' then this class will help.

Inserting Google Maps into my Web Page in asp.net

I am creating a web app. where I want be able to incorporate Google Maps into 1 of my pages.
From what I have read in other places, the easiest think is to place a web browser onto the form but there is no 'Web Browser' in the tool-box.
What I am trying to do is to insert a location into a textbox(ie. London) and insert a type of sport(ieCycling) and the resultant map shows up. Is there any other way in doing this in C# other than using the web browser tool.
Here is my code:
protected void btnSearch_Click(object sender, EventArgs e)
{
string sport = txtSport.Text;
string location = txtLocation.Text;
try
{
StringBuilder queryAddrress = new StringBuilder();
queryAddrress.Append("https://maps.google.ie/");
if (sport != string.Empty)
{
queryAddrress.Append(sport+","+"+");
}
if (location != string.Empty)
{
queryAddrress.Append(location + "," + "+");
}
Panel1.Navigate(queryAddrress.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error");
}
} protected void btnSearch_Click(object sender, EventArgs e)
{
string sport = txtSport.Text;
string location = txtLocation.Text;
try
{
StringBuilder queryAddrress = new StringBuilder();
queryAddrress.Append("https://maps.google.ie/");
if (sport != string.Empty)
{
queryAddrress.Append(sport+","+"+");
}
if (location != string.Empty)
{
queryAddrress.Append(location + "," + "+");
}
Panel1.Navigate(queryAddrress.ToString());
}
I tried to put the address into a panel but this is clearly wrong. Any help would be greatly appreciated!
It seems like you are very confused and mixing ASP.NET Web Forms with Windows Forms.
Specifically, MessageBox.Show() would open a Windows message box, not a browser window. And it would happen on the server side for whatever user your web server runs as. Probably not the desired intention. Also, you can't "put a web browser" onto a page. There is a WebBrowser control for Windows Forms, which embeds a minified version of Internet Explorer into a Windows application. But again, probably not what you want.
ASP.NET can be used as if it were just a normal HTML site. So find some Google Maps tutorials for HTML and follow those.

Categories

Resources