ASP Razor 网页 - 表单提交正在移动我的数据
ASP Razor Web Pages - Form Submit is Moving My Data
当我提交我的 Login
电子邮件(又名用户名)时,我想将其保留在我的 Login
表格中。
当我提交我的 Register
电子邮件(又名用户名)时,我想将其保存在我的 Register
表单中。
发生的事情是:当我提交 Login
表单时,它会将电子邮件(又名用户名)写入 Register
表单。请查看图片以了解正在发生的事情。
@{
Page.Title = "Login";
var username = "";
var password = "";
var errorMessage = "";
var confirmPassword = "";
var regMsg = "";
var confToken = "";
var emailBody = "";
var minPass = 2;
var maxPass = 5;
if (!IsPost) {
if (WebSecurity.IsAuthenticated) {errorMessage = String.Format("You are already logged in. (Username: {0})", WebSecurity.CurrentUserName);}
}
if(WebSecurity.IsAuthenticated){
errorMessage = String.Format("You are already logged in. (Username: {0})", WebSecurity.CurrentUserName);
}
if(IsPost){
// Login Form
if (Request.Form["loginSub"] != null){
regMsg = "";
username = Request["username"];
password = Request["password"];
if(WebSecurity.Login(username,password,true)){
Response.Redirect("~/Profile");
}
else
{
errorMessage = "Invalid Username or Password. Please try again.";
}
}
// Register Form
if (Request.Form["registerSub"] != null){
WebSecurity.Logout();
errorMessage = "";
username = Request["username"];
password = Request["password"];
confirmPassword = Request["confirmPassword"];
try {
var mail = new System.Net.Mail.MailAddress(username);
} catch {
regMsg += "<br>Invalid email format.";
}
if (password != confirmPassword) {regMsg += "</br>Passwords don't match.";}
if (WebSecurity.UserExists(username)) {regMsg += String.Format("</br>User '{0}' already exists.", username);}
if (password.Length < minPass || password.Length > maxPass) {regMsg += "</br>Password doesn't meet length requirement.";}
if (regMsg == "") {
WebSecurity.CreateUserAndAccount(username,password,null,false); //Switch with "true" token
regMsg = String.Format("{0} created.", username);
Response.Write("<script>alert('Email verification sent! Please check your email to activate your account.');</script>");
//Response.Write("<script>location.href = 'Default.cshtml';</script>");
}
}
}
}
<style>header {visibility: hidden;}</style>
<body>
<h1>MySite</h1>
<p>
@if(errorMessage != ""){<span>@Html.Raw(errorMessage)</span>}
@if(regMsg != ""){<span>@Html.Raw(regMsg)</span>}
</p>
<fieldset class="fs100">
<legend>Login</legend>
<form method="post" name="login">
@if(WebSecurity.IsAuthenticated){
<p>You are currently logged in as @WebSecurity.CurrentUserName.
<a href="@Href("~/Logout")">Log out</a>
</p>
}
<p>
<label for="username">Email Address:</label><br/>
<input type="text" name="username" id="username" value="" />
</p>
<p>
<label for="password">Password:</label><br/>
<input type="password" name="password" id="password" value="" />
</p>
<p>
<input type="submit" name="loginSub" value="Submit" />
</p>
<p><a href="@Href("~/Register")">Forgot Password?</a></p>
</form>
</fieldset>
<fieldset>
<legend>Register</legend>
<form method="post" name="register">
<p>
<label for="username">Email Address:</label><br/>
<input type="text" name="username" id="username" value='@Request["username"]' />
</p>
<p>
<label for="password">Password @minPass-@maxPass Characters:</label><br/>
<input type="password" name="password" id="password" value="" />
</p>
<p>
<label for="confirmPassword">Confirm Password:</label><br/>
<input type="password" name="confirmPassword" id="confirmPassword" value="" />
</p>
<p>
<input type="submit" name="registerSub" value="Register" />
</p>
</form>
</fieldset>
</body>
您的代码正在以第二种形式回应请求中的值。
如果您不希望它这样做,请更改您的代码以执行其他操作。
您可能希望将每个表单更改为具有唯一的字段名称。
当我提交我的 Login
电子邮件(又名用户名)时,我想将其保留在我的 Login
表格中。
当我提交我的 Register
电子邮件(又名用户名)时,我想将其保存在我的 Register
表单中。
发生的事情是:当我提交 Login
表单时,它会将电子邮件(又名用户名)写入 Register
表单。请查看图片以了解正在发生的事情。
@{
Page.Title = "Login";
var username = "";
var password = "";
var errorMessage = "";
var confirmPassword = "";
var regMsg = "";
var confToken = "";
var emailBody = "";
var minPass = 2;
var maxPass = 5;
if (!IsPost) {
if (WebSecurity.IsAuthenticated) {errorMessage = String.Format("You are already logged in. (Username: {0})", WebSecurity.CurrentUserName);}
}
if(WebSecurity.IsAuthenticated){
errorMessage = String.Format("You are already logged in. (Username: {0})", WebSecurity.CurrentUserName);
}
if(IsPost){
// Login Form
if (Request.Form["loginSub"] != null){
regMsg = "";
username = Request["username"];
password = Request["password"];
if(WebSecurity.Login(username,password,true)){
Response.Redirect("~/Profile");
}
else
{
errorMessage = "Invalid Username or Password. Please try again.";
}
}
// Register Form
if (Request.Form["registerSub"] != null){
WebSecurity.Logout();
errorMessage = "";
username = Request["username"];
password = Request["password"];
confirmPassword = Request["confirmPassword"];
try {
var mail = new System.Net.Mail.MailAddress(username);
} catch {
regMsg += "<br>Invalid email format.";
}
if (password != confirmPassword) {regMsg += "</br>Passwords don't match.";}
if (WebSecurity.UserExists(username)) {regMsg += String.Format("</br>User '{0}' already exists.", username);}
if (password.Length < minPass || password.Length > maxPass) {regMsg += "</br>Password doesn't meet length requirement.";}
if (regMsg == "") {
WebSecurity.CreateUserAndAccount(username,password,null,false); //Switch with "true" token
regMsg = String.Format("{0} created.", username);
Response.Write("<script>alert('Email verification sent! Please check your email to activate your account.');</script>");
//Response.Write("<script>location.href = 'Default.cshtml';</script>");
}
}
}
}
<style>header {visibility: hidden;}</style>
<body>
<h1>MySite</h1>
<p>
@if(errorMessage != ""){<span>@Html.Raw(errorMessage)</span>}
@if(regMsg != ""){<span>@Html.Raw(regMsg)</span>}
</p>
<fieldset class="fs100">
<legend>Login</legend>
<form method="post" name="login">
@if(WebSecurity.IsAuthenticated){
<p>You are currently logged in as @WebSecurity.CurrentUserName.
<a href="@Href("~/Logout")">Log out</a>
</p>
}
<p>
<label for="username">Email Address:</label><br/>
<input type="text" name="username" id="username" value="" />
</p>
<p>
<label for="password">Password:</label><br/>
<input type="password" name="password" id="password" value="" />
</p>
<p>
<input type="submit" name="loginSub" value="Submit" />
</p>
<p><a href="@Href("~/Register")">Forgot Password?</a></p>
</form>
</fieldset>
<fieldset>
<legend>Register</legend>
<form method="post" name="register">
<p>
<label for="username">Email Address:</label><br/>
<input type="text" name="username" id="username" value='@Request["username"]' />
</p>
<p>
<label for="password">Password @minPass-@maxPass Characters:</label><br/>
<input type="password" name="password" id="password" value="" />
</p>
<p>
<label for="confirmPassword">Confirm Password:</label><br/>
<input type="password" name="confirmPassword" id="confirmPassword" value="" />
</p>
<p>
<input type="submit" name="registerSub" value="Register" />
</p>
</form>
</fieldset>
</body>
您的代码正在以第二种形式回应请求中的值。
如果您不希望它这样做,请更改您的代码以执行其他操作。
您可能希望将每个表单更改为具有唯一的字段名称。