【问题标题】:How can I manually create an identity and authenticate them in MVC?如何在 MVC 中手动创建身份并对其进行身份验证?
【发布时间】:2014-01-25 19:36:00
【问题描述】:

我正在使用默认的 MVC 5 项目并保护以下方法,以便只有以“foobar”身份登录的用户才能访问它。这个特殊用户将存在于普通数据库之外(并在 web.config 中定义,但目前它是硬连线的)

    [Authorize(Users = "foobar")]
    public ActionResult Install()
    {
        //setup database;
        return View();
    }

我正在检查 foobar 的登录方法,如果检测到,我想登录它们,然后将它们重定向到安装操作。

 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
         if (model.UserName.ToLower().Trim() == "foobar" && model.Password == "pass")
                {
                  //create an Idenity named foobar and log them in
                  return RedirectToAction("Install");

【问题讨论】:

  • 只是好奇为什么要在普通数据库之外创建一个具有硬编码密码的特殊用户?默认用户存储(密码散列)中内置了许多保护措施,您不会使用此一次性帐户。

标签: asp.net-mvc forms-authentication


【解决方案1】:
        string username = model.username;
        string password = model.password;

        // This is where you do your super user check against the config file
        bool userValid = true; // compare id and password against config.
        // this is a bad idea, back door user is a security hole that can be exploited

        if (userValid)
        {
            //this will set the identity and the cookie for your user
            FormsAuthentication.SetAuthCookie(username, false);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多