【发布时间】:2015-09-14 08:27:34
【问题描述】:
我目前正在为我的实习(一年级comp. sci. student)开发一个 Intranet - 我使用了最后一个实习生代码并且不得不对其进行调试/做一些调整。到目前为止一切顺利;但我现在的工作是从 Windows 身份验证方法转变为基于表单的身份验证方法,而当涉及到它时,我非常迷茫。到目前为止,我已将 Web.config 中的身份验证模式更改为 Forms,但问题是:
通过添加
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" /> <!-- Was in comments -->
</authentication>
<authorization>
<deny users="?" />
</authorization>
到我的 Web.config 它会将我重定向到要求我输入信息的基本登录页面。 (由于授权)
但它仍然显示来自 Site.Master 的以下内容:
<div class="float-right">
Welcome <%: System.Environment.UserName %> - <a href='<%= Url.Action("MyProfile", "Employee") %>'>My Profile</a> | <%: Html.ActionLink("Logout", "Deco", "Home") %>
</div> <!-- Should only display if logged in -->
好像我还处于登录状态,我可以更改我的个人资料。
有没有办法只在我的基于表单的身份验证登录时才显示它?
编辑:此外,我应该怎么做才能从 Windows auth 迁移。到基于表单的?
EDIT2:这是我的 AccountController 中的代码:
[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
//
// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
WebSecurity.Logout();
//FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
【问题讨论】:
-
您是在检查用户是否已登录,然后显示该代码,还是没有检查就在那里?您在 System.Environment.UserName 中看到您的用户名了吗?
-
@freshbm : 好像没有验证,显示的是用户名。但似乎她没有显示(内网的)用户名,而是我的环境名称。
标签: c# asp.net-mvc forms-authentication windows-authentication