C# windows身份验证以获取MVC4中的用户列表
我的应用程序使用windows授权,但我手动指定具有以下访问权限的用户:C# windows身份验证以获取MVC4中的用户列表,c#,asp.net-mvc,asp.net-mvc-4,windows-authentication,C#,Asp.net Mvc,Asp.net Mvc 4,Windows Authentication,我的应用程序使用windows授权,但我手动指定具有以下访问权限的用户: [Authorize(Users = "domain\\userone, domain\\usertwo, domain\\userthree")] 我想知道是否可以在其中设置一个循环,以循环通过数据库调用返回的用户列表,例如,可以从数据库获取用户NT帐户列表的快速模型: List<string> users = new List<String>(); SqlConnection con = n
[Authorize(Users = "domain\\userone, domain\\usertwo, domain\\userthree")]
我想知道是否可以在其中设置一个循环,以循环通过数据库调用返回的用户列表,例如,可以从数据库获取用户NT帐户列表的快速模型:
List<string> users = new List<String>();
SqlConnection con = new SqlConnection(Properties.Default.ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = @"SELECT NT_ACCOUNT FROM USERS";
SQLDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
users.Add(reader.GetValue(reader.FieldCount));
}
或者,使用linq查询可能会更简单,我不确定,但上面的代码只是一个猜测。修改AuhorizeCore方法:
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
//_usersSplit = ListOfAuthorizedNames
if ((_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) && (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)))
{
return false;
}
return true;
}
摘自:修改AuhorizeCore方法:
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
//_usersSplit = ListOfAuthorizedNames
if ((_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) && (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)))
{
return false;
}
return true;
}
摘自:修改AuhorizeCore方法:
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
//_usersSplit = ListOfAuthorizedNames
if ((_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) && (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)))
{
return false;
}
return true;
}
摘自:修改AuhorizeCore方法:
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
//_usersSplit = ListOfAuthorizedNames
if ((_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) && (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)))
{
return false;
}
return true;
}
摘自:如果要允许所有用户,为什么不使用[Authorize]呢?@Sunny因为用户列表已经是一个表,但不是该列表中的每个人都应该被允许访问,如果门户中有一些允许或不允许访问的功能,那就太好了。好的,您需要修改AuthorizeCore方法,然后查看此链接:如果您想允许所有用户,为什么不只使用[Authorize]?@Sunny,因为用户列表已经是一个表,但该列表中的每个人都不应该被允许访问,如果门户中有一些允许或不允许访问的功能,那就太好了。好的,您需要修改AuthorizeCore方法,然后查看此链接:如果您想允许所有用户,为什么不只使用[Authorize]?@Sunny,因为用户列表已经是一个表,但该列表中的每个人都不应该被允许访问,如果门户中有一些允许或不允许访问的功能,那就太好了。好的,您需要修改AuthorizeCore方法,然后查看此链接:如果您想允许所有用户,为什么不只使用[Authorize]?@Sunny,因为用户列表已经是一个表,但该列表中的每个人都不应该被允许访问,如果门户中有一些允许或不允许访问的功能,那就太好了。好的,你需要修改AuthorizeCore方法,请参见此链接:尝试一下,它应该是解决方案尝试一下,它应该是解决方案尝试一下,它应该是解决方案尝试一下,它应该是解决方案尝试一下,它应该是解决方案