Read Post Data submitted to ASP.Net Form - c#

I have a working login form in an asp.net application. Standard stuff with a username and password text box and a button to process the login. Works fine.
I have a new requirement to allow the user to input the username and password from a separate plain html page that is not a part of my asp.net application. I plan on achieving this using standard html - form, input, submit button etc. The form action will be the URL of my asp.net login page and its method will be POST.
What I want to do in the C# code behind page of the asp.net login form, presumably in the Page_Load event, is to check if the request for the page contains a username and password value being passed in. If it does then I need to read those values and process the login as if someone had clicked the login button on the asp.net page. If not then I will display the login form as usual.
How do I check for the existence of, and read, the username and password values in the request for my page.

Read the Request.Form NameValueCollection and process your logic accordingly:
NameValueCollection nvc = Request.Form;
string userName, password;
if (!string.IsNullOrEmpty(nvc["txtUserName"]))
{
userName = nvc["txtUserName"];
}
if (!string.IsNullOrEmpty(nvc["txtPassword"]))
{
password = nvc["txtPassword"];
}
//Process login
CheckLogin(userName, password);
... where "txtUserName" and "txtPassword" are the Names of the controls on the posting page.

if (!string.IsNullOrEmpty(Request.Form["username"])) { ... }
username is the name of the input on the submitting page. The password can be obtained the same way. If its not null or empty, it exists, then log in the user (I don't recall the exact steps for ASP.NET Membership, assuming that's what you're using).

NameValueCollection nvclc = Request.Form;
string uName= nvclc ["txtUserName"];
string pswod= nvclc ["txtPassword"];
//try login
CheckLogin(uName, pswod);

Related

I need to populate current username in the "Username" field of Identity Server custom login page

In the link below for Identity Server 3,I have to fill username and password to login.
https://github.com/IdentityServer/IdentityServer3/blob/master/source/Core/Services/DefaultViewService/PageAssets/login.html
But,what I need is that username should be filled automatically before login page flashes on screen with Window's username of the system.For this,I tried the following which I have presented here as well as in a answer to this question:
But that works only in Internet explorer because it has ActiveX which is only supported in Internet explorer.I need to somehow pass
(System.Environment.Username) <-gives me the current user which I need to pass into login page
<script>
function GetUsername()
{
var wshshell = new ActiveXObject("wscript.shell");
var username = wshshell.ExpandEnvironmentStrings("%username%");
document.getElementById("username").value = username;
}
</script>
I called this method from <body lang="en" onload="GetUserName()"> but this only gives current username in Internet explorer. Please someone suggest me better answer if they have any idea.

Trying to fire/call OnLoggedIn event from Page_Load event

I am implementing ASP.NET User Membership authentication in a website.
I am using ASP.NET's control to
authenticate user,
sign up new user and
change password to change the authenticated user's password.
My functionality is:
On Sign Up page, I have User name and Email as inputs and I have set theAutoGeneratePassword property to true.
Once successful registration system sends out an email to that user with a URL in email (like http://localhost:xxxx/Login.aspx?user=abc10&pwd=something).
My issue is, when user clicks above link, he should be redirected automatically to change password page to change the password.
So, for redirection, I am trying to call the OnLoggedIn event in page_Load.
I have tried below calls from page_Load event.
Login1_LoggedIn(sender.GetType(), null)
login.LoggedIn += new EventHandler(Login1_LoggedIn)
But the event is not firing, can anybody help?
Finally I figured that out. As I had to work out two scenarios,
I. The user should be able to login from the login page.
II. And when user should be able to redirect to next page following login when he/she clicks the link they have received in their email.
So the first point is very straight forward.
For the second point, I created a method which has all code logic to authenticate the user. And called this method in page_load() event and from OnLoggedIn event.
And in page load, I wrote below code to simulate login event to redirect user directly to next page.
protected void Page_Load(object sender,EventArgs e)
{
string username = usernameTextbox.Text.Trim();
**FormsAuthentication.SetAuthCookie(username, true);**
callLogin();// this method where in my user authentication logic resides
}

How to get current username information from MySql Database ,C#

I have an application with login form, and I want after the login to show on a label or something , the username and other information from my database with that user.
For example , after you login with username "admin" , to show me on next form a text "Welcome Administrator!" . Or if I login with username "john", to show me on next form a text "Welcome John Snow!" .
How I can do that ?
You'd have to keep track of a session identifier and use that to track a user through a current session and you could then display that on your GUI through whatever language/api you are using in your application. Alternatively if you don't want to or need to keep track of your user past the login page you could just forward that to the class that handles the next page by allowing the next class to inherit the previous.
However it's hard to get much more specific without more details on your project.
Best way is to put the UserName variable in Session and access it in the next page.
Pseudo Code...
In the login page after authentication check write this (assuming 2nd page is Home.aspx):
Session["USER_NAME"] = userName;
Respinse.Redirect("Home.aspx");
In Home.aspx page_load function, write this to fetch the name from session and display, and redirect user to login page if no session value present.
if(Session["USER_NAME"] != null)
{
lblUserName.Text = Session["USER_NAME"].ToString();
}
else
{
Respinse.Redirect("Login.aspx");
}

login and userID toolbar

I looking for a tutorial of how i can make my wesite toobar show the name of the user after he login so he can just press on his name in the toolbar and move him to his personal page such as your user name and your photo on the top of the forums.
Many thanks
It can be done in several different ways, however this is probably the simplest. Save the username in a session variable:
Session["UserName"] = txtUsername.text;
and in all the other pages , you will need to have a Label that shows the name of the user who is currently logged in. You can get the value from the "session" in this way:
labelUser.Text = Session["UserName"].Tostring();
Do not forget to set the session to null when the user logs out:
Session["Username"] = null
How to redirect on his page, just use again the value of session variable to redirect to his page.

Using C# Windows Application. How to Identify logging-in to a website was successful using Visual Studio's WebBrowser?

I have created a c# windows application that have provided text box for user id and password and upon clicking submit button it will automatically log-in an eBay account to www.ebay.com using webbrowser component. Logging in seems to be easy if the inputted username and password was correct. And now what i want to accomplish with your kind help is:
Every time the user inputs a wrong username or password on the windows application a message box will notify the user that the username and password was incorrect. And that the logging in to the website was unsuccessful.
Here is my code for WebBrowser's event DocumentComplete.
//webTest is the name of my web browser
private void webTest_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//boolean login variable checks if the button login was clicked.
if (login == true)
{
//checks if the url is on the login page of eBay.
if (webTest.Url.AbsoluteUri == loginPage)
{
HtmlDocument doc = webTest.Document;
HtmlElement username = doc.GetElementById("userid");
HtmlElement password = doc.GetElementById("pass");
HtmlElement submit = doc.GetElementById("but_sgnBt");
username.SetAttribute("value", txtUser.Text);
password.SetAttribute("value", txtPassword.Text);
submit.InvokeMember("click");
login = false;
}
//else the webbrowser webtest will navigate to the login page,
else
webTest.Navigate(loginPage);
}
}
Thank you for all the reply i really appreciate it. May God Bless c#!
Since webTest_DocumentCompleted will be called whenever a page is fully loaded, you should be able to detect if you were redirected from the login page to another page. For instance, the login page for eBay UK is located on the domain https://signin.ebay.co.uk/. A successful login transfers me to http://my.ebay.co.uk/ instead.
You could check for this transfer in the webTest_DocumentCompleted method. If it did happen, you were logged in. If not, you were not logged in (and can show a message box to alert the user).

Categories