Windows 使用 MVC 进行身份验证。 Windows 不同的身份验证 "accesslevels"

Windows Authentication with MVC. Windows Authentication with different "accesslevels"

我是 MVC 和身份验证的新手。

我们正在开发将使用 Windows 身份验证的 MVC 应用程序。我们首先在 HTML 中使用 razor 解决了这个问题,如下所示:

@if (HttpContext.Current.User.IsInRole("somerole") || HttpContext.Current.User.IsInRole("somerole") || HttpContext.Current.User.IsInRole("somerole"))

但这不会在很长一段时间内起作用 运行 因为我们希望能够 add/remove 在数据库中某个 windows 授权应该能够 see/not看到某个视图。换句话说,我们需要它比上面的例子更多 flexible/dynamic。

假设我们的用户数据库看起来像这样:

USER
string windowsauth
int accesslevel

如果我们有一个用户具有 windowsauth "Domain User" 和 accesslevel 1,我希望该用户能够看到所有页面。

另一个拥有 windowsauth "Other user" 和访问级别 2 的用户将只能看到某些页面。

我们不知道如何解决这个问题,我希望我的问题足够清楚。任何帮助都会很棒!

您可以创建一个基础模型,其中包含当前视图的授权角色列表。然后你可以这样做:

@if(Model.AuthRoles.Any(r=>HttpContext.Current.User.IsInRole(r))

因此您的基本模型可能如下所示:

public class BaseModel{
    List<string> AuthRoles {get;set;}

    public BaseModel(List<string> authorisedRoles){
        AuthRoles = authorisedRoles;
    }

    public BaseModel(){
         AuthRoles = new List<string>();
    }
}

然后当您填充模型时,您可以从数据库中加载授权角色。

此时也对角色执行检查并进行任何必要的重新定向。

您可以使用类似的前提在您的站点中构建导航栏。