【问题标题】:how authorization asp(mvc) project from controller?如何从控制器授权 asp(mvc) 项目?
【发布时间】:2016-11-02 20:25:25
【问题描述】:

我是 asp 新手,我为我的 web 项目创建了一个登录页面,但我设置了身份验证,但我无法为我的项目设置授权!我看到很多这样的链接Authentication and Authorization in ASP.NET Web API 但无法在我自己的项目中实现这些,我不知道我必须从哪里开始?! 谢谢你的帮助!

这是我的控制器:

public class AuthenticationController : Controller
{
    private modelLayOut mLO = new modelLayOut();
    public bool existBool = false; 
    // GET: Authentication
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult applicantAuthentication()
    {            
        return View("ApplicantAuthentication");
    }
    public ActionResult applicantIsExist()
    {
        return View("applicantIsExist");
    }
    public ActionResult applicantPassIsWrong()
    {
        return View("applicantPassIsWrong");
    }
    public ActionResult applicantNotExist()
    {
        return View("applicantNotExist");
    }
    [HttpPost]
    public ActionResult applicantCreate(string Username, string Password, string RepeatPassword)
    {
        if (mLO.applicantExistCheck(Username))
        {
            return View("applicantIsExist");
        }
        else
        {
            mLO.insertNewApplicant(Username, Password);
            return View("ApplicantAuthentication");
        }
    }
    [HttpPost]
    public ActionResult applicantAccess(string Username, string Password)
    {
        if (mLO.applicantAccess(Username, Password))
        {
            return RedirectToAction("Home", "Home");
        }
        else
        {
            if (mLO.applicantExistCheck(Username))
            {
                return View("applicantPassIsWrong");
            }
            else
            {
                return View("applicantNotExist");
            }
        }
    }

    //agency part
    public ActionResult agencyAuthentication()
    {
        return View("AgencyAuthentication");
    }
    public ActionResult agencyPassIsWrong()
    {
        return View("agencyPassIsWrong");
    }
    public ActionResult agencyNotExist()
    {
        return View("agencyNotExist");
    }
    [HttpPost]
    public ActionResult agencyAccess(string Username, string Password)
    {
        if (mLO.agencyAccess(Username, Password))
        {               
            return RedirectToAction("Home", "Home");
        }
        else
        {
            if (mLO.agencyExistCheck(Username))
            {
                return View("agencyPassIsWrong");
            }
            else
            {
                return View("agencyNotExist");
            }
        }
    }

    //webAdmin
    public ActionResult webAdminAuthentication()
    {
        return View("WebAdminAuthentication");
    }
    public ActionResult webAdminAccessWrong()
    {
        return View("webAdminAccessWrong");
    }
    [HttpPost]
    public ActionResult webAdminAccess(string Username, string Password)
    {
        if (mLO.webAdminAccess(Username, Password))
        {
            Session["Username"] = Username;
            return RedirectToAction("webAdminPage", "Admin");
        }
        else
        {
            return View("webAdminAccessWrong");
        }
    }

【问题讨论】:

    标签: asp.net-mvc login asp.net-mvc-5 asp.net-authorization


    【解决方案1】:

    您需要完全了解ASP.NET 5 Identity model(查看herehere)。然后,您应该通过适合您项目的任何更改来实现它。 ASP.NET 5 Identity 最重要的事情之一是它的简单性和灵活性,可用于不同的用户类型和可访问性,只需对方法使用注释即可。如果您以前使用过SQL Membership,请查看here 以了解如何从 SQL 成员身份迁移到 ASP.NET 身份。或者,如果您以前使用过 ASP.NET Membership,请查看 here 以了解如何从 ASP.NET 成员身份迁移到 ASP.NET 身份。 关于您在how say: "welcome PERSON NAME" ? 上的问题,在实现 ASP.NET 5 Identity 后,您只需要拥有

    System.Web.HttpContext.Current.User.Identity.Name

    在任何你需要的地方!

    【讨论】:

      【解决方案2】:

      如果您想对整个控制器进行授权,只需在控制器上设置 Authorize 属性:

      [Authorize]
      public class AuthenticationController : Controller
      {
      
      }
      

      如果您想对单个操作进行授权:

      public class AuthenticationController : Controller
      {
          [Authorize]
          public ActionResult Index()
          {
              ViewBag.Message = "Welcome, " + HttpContext.User.Identity.Name;
          }
      }
      

      编辑:只有经过身份验证的用户才能浏览授权的方法或控制器

      【讨论】:

      • 您好,感谢您的帮助!我想知道的是:我的项目如何在同时使用我网站的两个人之间设置不同? [授权]就够了吗?或者我需要添加一些东西!例如登录后怎么说:“welcome PERSON NAME”?
      • 关于经过身份验证的用户的所有信息都可以在 System.Web.HttpContext.Current.User.Identity 对象中找到。
      • 希望这个答案对你有帮助
      猜你喜欢
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      相关资源
      最近更新 更多