ASP.NET MVC3 的最简单身份验证

发布于 2024-12-16 23:03:02 字数 261 浏览 1 评论 0原文

我一直坚持简单的事情:(

我有一个带有 /Admin/ URL 的 ASP.NET MVC3 应用程序。我在那里放置了 [Authorize] 属性,这样就可以了。但现在我需要简单的事情:只是限制对它的访问只有一个用户名/密码。

我不想使用 ASP.NET 表单授权创建数据库,并且需要它在我的开发 PC 上使用 Visual Studio 和 IIS7 服务器

来快速完成此操作 。放到web.config 使其与“admin/p4ssw0rd”对一起工作?

I've stuck with simple thing :(

I have an ASP.NET MVC3 app with /Admin/ URL. I put [Authorize] attribute there, it's OK with that. But now I need simple thing: just to limit access to it with only one username/password.

I don't want create database with ASP.NET forms authorization, and I need it to work on my development PC with Visual Studio, and IIS7 server.

What's the best way to do this fast? What I have to put to web.config to make it work with "admin/p4ssw0rd" pair?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2024-12-23 23:03:04

请参阅:这个问题

您可以明确指定您的用户名和密码(如果您希望使用纯文本密码,请注意解决方案。

see: this question

You can specify your username and password explicitly (please note the solution if you wish to use the password in plain text.

丑丑阿 2024-12-23 23:03:04

当您创建 ASP.NET MVC 3 应用程序时,Visual Studio 添加了一个 AccountController。只需修改 LogOn 操作,即可手动执行验证,而不是在数据库中查找:

public class AccountController : Controller
{

    ...

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            // Here you can check the username and password against any data
            // store you want
            if (model.UserName == "admin" && model.Password == "p4ssw0rd")
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

    ...

}

When you created the ASP.NET MVC 3 application, Visual Studio added an AccountController. Simply modify the LogOn action so that instead of looking in database you perform the verification manually:

public class AccountController : Controller
{

    ...

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            // Here you can check the username and password against any data
            // store you want
            if (model.UserName == "admin" && model.Password == "p4ssw0rd")
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

    ...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文