C# 读取提交到 ASP.Net 表单的 Post 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/564289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:20:52  来源:igfitidea点击:

Read Post Data submitted to ASP.Net Form

c#asp.netpost

提问by daveywc

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.

我在 asp.net 应用程序中有一个有效的登录表单。带有用户名和密码文本框以及用于处理登录的按钮的标准内容。工作正常。

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.

我有一个新要求,允许用户从一个单独的纯 html 页面输入用户名和密码,该页面不是我的 asp.net 应用程序的一部分。我计划使用标准的 html - 表单、输入、提交按钮等来实现这一点。表单操作将是我的 asp.net 登录页面的 URL,它的方法将是 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.

我想在 asp.net 登录表单的 C# 代码隐藏页面中做的事情,大概是在 Page_Load 事件中,是检查页面请求是否包含传入的用户名和密码值。如果是,那么我需要读取这些值并处理登录,就像有人单击了 asp.net 页面上的登录按钮一样。如果没有,那么我会像往常一样显示登录表单。

How do I check for the existence of, and read, the username and password values in the request for my page.

如何检查我的页面请求中的用户名和密码值是否存在并读取。

采纳答案by Cerebrus

Read the Request.Form NameValueCollection and process your logic accordingly:

阅读 Request.Form NameValueCollection 并相应地处理您的逻辑:

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 Namesof the controls on the posting page.

... 其中“txtUserName”和“txtPassword”是发布页面上控件的名称

回答by John Sheehan

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

username 是提交页面上输入的名称。密码可以通过同样的方式获得。如果它不为 null 或为空,则它存在,然后登录用户(我不记得 ASP.NET Membership 的确切步骤,假设这是您正在使用的)。

回答by gdmanandamohon

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