【发布时间】:2014-06-25 15:14:44
【问题描述】:
我正在用 asp.net 4.0 C# 开发一个网站。我使用 mvc 4.0 我必须实现一个表单,但是当我提交它时。我想执行某种方法,但我不知道如何。
浏览量:
<FORM action="...?..." method="post">
<div class="identification">
id
@Html.TextBox("id")
password
@Html.Password("password")
</div>
<input type="submit" value="login" />
</FORM>
控制器(名为 loginControllers.cs)
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected Boolean Login_Click(object sender, EventArgs e)
{
string p = "LDAP://wrexham.ac.uk";
// string p = "LDAP://wxmdc1.wrexham.local";
string u = id.text;
string pw = password.Text;
if (AuthenticateUser(p, u, pw))
{
FormsAuthentication.SetAuthCookie(u, false);
//FormsAuthentication.Authenticate()
//FormsAuthentication.RedirectFromLoginPage(txtUser.Text, chkPersistLogin.Checked)
//FormsAuthentication.RedirectFromLoginPage(u, false);
//FormsAuthentication.SetAuthCookie()
Response.Write("True");
return true;
}
else
{
Response.Write("false");
return false;
}
}
protected bool AuthenticateUser(string path, string user, string pass)
{
//Simple Active Directory Authentication Using LDAP and ASP.NET
DirectoryEntry de = new DirectoryEntry(path, user, pass, AuthenticationTypes.Secure);
try
{
//run a search using those credentials.
//If it returns anything, then you're authenticated
DirectorySearcher ds = new DirectorySearcher(de);
ds.FindOne();
return true;
}
catch (Exception ex)
{
//otherwise, it will crash out so return false
Response.Write(ex.Message);
return false;
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
}
}
【问题讨论】:
-
您提供的代码绝对不是用于 MVC 控制器的。
标签: c# asp.net-mvc