我正在使用 C# 和 SQL Server 2005 开发一个 ASP.Net MVC 3 应用程序。我正在使用具有代码优先方法的实体框架。
我有一个 LOG ON(连接)接口,它与我的基地相关,我有一个 USER 表(包含登录名 + 密码)。
这是连接的视图:LogonPartial.acx(从 UserViewModel 强类型化的部分视图)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.ViewModels.UserViewModel>" %>
<%
if (Request.IsAuthenticated) {
%>
Welcome <strong><%: Page.User.Identity.Name %></strong>!
[ <%: Html.ActionLink("Log Off", "LogOff", "Account") %> ]
<%
}
else {
%>
[ <%: Html.ActionLink("Log On", "LogOn", "Account") %> ]
<%
}
%>
当连接成功时:我只有“登录”链接。连接失败时:页面为空
这是控制器:
[ChildActionOnly]
public ActionResult LogedInUser()
{
var user = new UserViewModel();
if (Request.IsAuthenticated)
{
user.Nom_User = User.Identity.Name;
}
return PartialView(user);
}
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 LogedInUser(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;
}