112

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.

3 Answers 3

156

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.

5
  • 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 Sep 6, 2012 at 19:42
  • 2
    Curious why iterate over a namevaluecollection rather than check the request directly for each control name?
    – Howiecamp
    Feb 17, 2014 at 18:31
  • @Howiecamp ever found the answer to that question? I'm intrigued aswell. Jun 8, 2016 at 14:28
  • 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.
    – Callat
    Nov 25, 2017 at 6:37
  • make sure that your form controls have set the runat=server attribute otherwise they are not sent as part of the request Nov 2, 2021 at 17:43
15
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).

3
NameValueCollection nvclc = Request.Form;
string   uName= nvclc ["txtUserName"];
string   pswod= nvclc ["txtPassword"];
//try login
CheckLogin(uName, pswod);
1
  • Give a proper reference of you answer so that it's more feasible.
    – user6132703
    Dec 14, 2016 at 5:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.