Read Post Data submitted to ASP.Net Form

228,720

Solution 1

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.

Solution 2

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).

Solution 3

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

Related videos on Youtube

rbrtl
Author by

rbrtl

Experienced software anaylyst/developer running a small team developing mainly web based and mobile software. Main technologies used are C#, Objective C, Java, MS SQL Server, Visual Studio, ASP.Net MVC, JQuery, Javascript, WPF, WCF, breezejs, durandal and angularjs

Updated on July 08, 2022

Comments

  • rbrtl
    rbrtl almost 2 years

    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.

  • Fallenreaper
    Fallenreaper over 11 years
    What about byte arrays? Would you have a string like that and then convert it to a byte array, or what? Im uploading a file to the serve.r
  • Howiecamp
    Howiecamp over 10 years
    Curious why iterate over a namevaluecollection rather than check the request directly for each control name?
  • tfrascaroli
    tfrascaroli almost 8 years
    @Howiecamp ever found the answer to that question? I'm intrigued aswell.
  • Admin
    Admin over 7 years
    Give a proper reference of you answer so that it's more feasible.
  • Callat
    Callat over 6 years
    THIS! I was debugging for a solid 5 hours trying to figure out why the hell I can't get the data off this form.
  • Mauricio Gracia Gutierrez
    Mauricio Gracia Gutierrez over 2 years
    make sure that your form controls have set the runat=server attribute otherwise they are not sent as part of the request