CodeGo.net>如何使用SQL Server 2005,ASP.NET MVC和EF进行身份验证
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CodeGo.net>如何使用SQL Server 2005,ASP.NET MVC和EF进行身份验证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2095字,纯文字阅读大概需要3分钟。
内容图文
![CodeGo.net>如何使用SQL Server 2005,ASP.NET MVC和EF进行身份验证](/upload/InfoBanner/zyjiaocheng/881/8e1fca4a71d346fc97eac2c8b15a9171.jpg)
我正在使用C#和SQL Server 2005开发ASP.Net MVC 3应用程序.我在以代码优先方式使用Entity Framework.
我有一个用于登录(连接)的界面,但它与我有一个USER表(包含登录密码)的基础无关.
我试图像这样更改AccountController中的LogON函数:
private GammeContext db = new GammeContext();
[AcceptVerbs(HttpVerbs.Post)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
Justification = "Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string Matricule, string passWord, bool rememberMe, string returnUrl)
{
if (!ValidateLogOn(Matricule, passWord))
{
//return View();
Connection(Matricule, passWord);
}
FormsAuth.SignIn(Matricule, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
public int Connection(string Matricule, string passWord)
{
users = db.Users.ToList();
foreach (User u in users)
{
if ((u.Matricule == Matricule) && (u.passWord == passWord))
{
return View();
}
else
{
return Redirect(returnUrl);
}
}
}
这段代码有什么问题吗?
有一个我无法修复的错误
The name ‘users’ does not exist in the current context
解决方法:
您尚未声明用户,因此需要在其前面放置一个类型:
List<User> users = db.Users.ToList();
另外,您的Connection函数返回类型为int,但是您试图返回View?您应该将返回类型更改为ActionResult.另外,当您直接返回循环时,您只会经历该循环的第一次迭代吗?如果该列表中没有用户,则还需要全部捕获.
尝试这个:
public ActionResult LogOn(string Matricule, string passWord, bool rememberMe, string returnUrl)
{
if (!ValidateLogOn(Matricule, passWord))
{
return Connection(Matricule, passWord, returnUrl);
}
FormsAuth.SignIn(Matricule, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
public ActionResult Connection(string Matricule, string passWord, string returnUrl)
{
List<User> users = db.Users.ToList();
ActionResult output = null;
if (users.Any())
{
foreach (User u in users)
{
if ((u.Matricule == Matricule) && (u.passWord == passWord))
{
output = View();
}
}
}
else
{
output = Redirect(returnUrl);
}
return output;
}
内容总结
以上是互联网集市为您收集整理的CodeGo.net>如何使用SQL Server 2005,ASP.NET MVC和EF进行身份验证全部内容,希望文章能够帮你解决CodeGo.net>如何使用SQL Server 2005,ASP.NET MVC和EF进行身份验证所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。