【发布时间】:2013-05-10 08:38:32
【问题描述】:
我正在使用 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;
}
【问题讨论】:
-
首先你们从来没有使用过你的 UserViewModel。其次,我不确定您所说的“连接成功”是什么意思(我猜是登录成功),但如果是这种情况,您的用户身份验证不正确。
-
是的,登录成功就是我的意思。我认为身份验证没问题,因为当我输入基础中存在的值时,它会将我带到带有 LogOn 链接的页面。但是,在相反的情况下,它会将我带到一个空白页面
-
这个逻辑应该在你的控制器而不是你的页面中。您的帐户控制器中的代码是什么样的?
-
@KOL 请看一下编辑
-
我没有看到您的操作链接在您添加的代码中调用的 LogOn 方法或 LogOff 方法
标签: c# sql-server asp.net-mvc-3 visual-studio-2010