我已搜查这里很多帖子关于自定义用户身份验证,但没有已经解决了我所有的顾虑ASP.NET MVC和登录认证
我新的ASP.NET MVC,并使用传统的ASP.NET(Web窗体)但不知道如何为使用ASP.NET MVC的用户构建登录/身份验证机制。
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string userName = Login1.UserName;
string password = Login1.Password;
bool rememberUserName = Login1.RememberMeSet;
if (validateuser(userName, password))
{
//Fetch the role
Database db = DatabaseFactory.CreateDatabase();
//Create Command object
System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("sp_RolesForUser");
db.AddInParameter(cmd, "@Uid", System.Data.DbType.String, 15);
db.SetParameterValue(cmd, "@Uid", Login1.UserName);
System.Data.IDataReader reader = db.ExecuteReader(cmd);
System.Collections.ArrayList roleList = new System.Collections.ArrayList();
if (reader.Read())
{
roleList.Add(reader[0]);
string myRoles = (string)roleList[0];
//Create Form Authentication ticket
//Parameter(1) = Ticket version
//Parameter(2) = User ID
//Parameter(3) = Ticket Current Date and Time
//Parameter(4) = Ticket Expiry
//Parameter(5) = Remember me check
//Parameter(6) = User Associated Roles in this ticket
//Parameter(7) = Cookie Path (if any)
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
DateTime.Now.AddMinutes(20), rememberUserName, myRoles, FormsAuthentication.FormsCookiePath);
//For security reasons we may hash the cookies
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
// add the cookie to user browser
Response.Cookies.Add(cookie);
if (HttpContext.Current.User.IsInRole("Administrators"))
{
Response.Redirect("~/Admin/Default.aspx");
}
else
{
string returnURL = "~/Default.aspx";
// get the requested page
//string returnUrl = Request.QueryString["ReturnUrl"];
//if (returnUrl == null)
// returnUrl = "~/Default.aspx";
Response.Redirect(returnURL);
}
}
}
}
protected bool validateuser(string UserName, string Password)
{
Boolean boolReturnValue = false;
//Create Connection using Enterprise Library Database Factory
Database db = DatabaseFactory.CreateDatabase();
//Create Command object
DbCommand cmd = db.GetStoredProcCommand("sp_ValidateUser");
db.AddInParameter(cmd, "@userid", DbType.String, 15);
db.SetParameterValue(cmd, "@userid", Login1.UserName);
db.AddInParameter(cmd, "@password", DbType.String, 15);
db.SetParameterValue(cmd, "@password", Login1.Password);
db.AddOutParameter(cmd, "@retval", DbType.Int16, 2);
db.ExecuteNonQuery(cmd);
int theStatus = (System.Int16)db.GetParameterValue(cmd, "@retval");
if (theStatus > 0) //Authenticated user
boolReturnValue = true;
else //UnAuthorized...
boolReturnValue = false;
return boolReturnValue;
}
我真的不知道如何将该ASP.NET代码翻译成MVC-esque体系结构;而且我仍然对如何在ASP.NET MVC中实现身份验证感到茫然。
我需要做什么?我如何在ASP.NET MVC中实现上述代码?那些代码中缺少什么?
ASPNET MVC附带了一个完全成熟的成员组成,哪些功能是烂摊子?您可以阅读http://www.codeplex.com/McCMembership开始 – 2009-12-28 17:54:30
非常赞同Jay Zeng。不要(错误地)重新创建成员资格:http://blogs.teamb.com/craigstuntz/2009/09/09/38390/ – 2009-12-28 17:59:35
那么它可以像我想要的那样定制?因为我的用户表和角色表是不同的,因为我打算开发一个HELP DESK应用程序,并且注册不是针对公众的,因为只有管理员可以创建用户。我可以自定义这种方式吗? – user239684 2009-12-28 18:36:27