【问题标题】:Form authentication method in my ASP.NET page我的 ASP.NET 页面中的表单身份验证方法
【发布时间】: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


【解决方案1】:

我不确定您是否正在检查用户是否已登录应用程序以显示该段代码。如果没有,那么您可以先检查用户是否已登录,然后使用Request.IsAuthenticatedUser.Identity.IsAuthenticated 显示该html:

@if(Request.IsAuthenticated) {
<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 -->
}

或:

@if (User.Identity.IsAuthenticated) {
  <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 -->
}

编辑:

您还应该更改您的登录方法以使用FormsAuthentication.Authenticate 方法进行用户身份验证:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && FormsAuthentication.Authenticate(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);
    }

也在您的注销方法中使用FormsAuthentication.SignOut();

【讨论】:

  • 这确实会停止显示,但问题是即使它不显示它似乎我仍然在登录。(可能通过 Windows 身份验证)。编辑:只有当我在 Web.config 中取出 时才允许我进入,否则它总是将我重定向到登录页面。
  • 我刚刚添加了。
  • 我不知道,我想我现在应该做的是将密码/帐户通道存储在数据库中,然后检查并让用户进入。到目前为止,因为我已经使用Windows 身份验证。我不需要它,默认情况下我已登录到我的帐户。我会想办法做到这一点。
【解决方案2】:

对于表单身份验证,您应该使用User.Identity.Name 来获取当前登录用户的用户名。还要检查User.Identity.IsAuthenticated 以查看当前用户是否已登录。

但是,根据项目的创建方式,您可能需要在后台添加代码来处理登录。如果您在创建项目时启用表单身份验证,ASP.NET 将为您创建它,但如果项目是使用 Windows 身份验证创建的,则可能不会这样做。

以下教程很好地概述了如何配置和实现表单身份验证: http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多