猿问

通过数据注释进行身份验证和授权

我有一个数据库表,其中包含应允许对其应用程序进行一般访问的所有用户。除此之外,还有一个bool列IsAdministrator,如果为true,则允许该用户访问特定操作/视图,如创建新用户,删除现有对象等。


我的目标是创建一种方法,以便如果用户在不是管理员的情况下尝试访问创建操作/视图,则会将其返回到“错误视图”页面。


我知道我可以做这样的事情:


    public ActionResult Personnel()

    {

        // get user who is logged in

        var personLoggedIn = User.Identity.Name.Split('\\')[1]; // Intranet application.. so the domain name comes before the username hence the split


        if(_context.UserTable.Single(x => x.UserLogon == personLoggedIn).IsAdministrator == false)

        {

            return View("Error");

        }


        // more code below

    }

我想知道是否要代替将其复制并粘贴到每个不同的actionresult中。我可以将其放入数据注释中,并用该数据注释装饰每个不同的actionresult吗?


慕的地8271018
浏览 108回答 2
2回答
随时随地看视频慕课网APP
我要回答