![](/img/trans.png)
[英]connection string from web.config fails where identical hard-coded string succeeds (ASP.NET with C#)
[英]asp.net How do I reference authorized users hard coded in web.config in my code
我正在内联网上构建一个网站,其中一个目录只能由硬编码的授权用户访问。 它们在web.config中定义。 它看起来与此类似。
<location path="admin">
<system.web>
<authorization>
<allow users="user1"/>
<allow users="user2"/>
<allow users="user3"/>
<allow users="user4"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
我想要的是创建一个这个目录的链接,只有那些用户才能看到......目前,为了构建链接,我正在重新检查那里的windows用户名并再次对它们进行硬编码......
<%
if (HttpContext.Current.User.Identity.Name == "user1" ||
HttpContext.Current.User.Identity.Name == "user2" ||
HttpContext.Current.User.Identity.Name == "user3" ||
HttpContext.Current.User.Identity.Name == "user4")
{
Response.Write("<a href='admin/Default.aspx'>Admin Site</a>");
}
%>
但我想要做的是从webiconfig文件中引用我的列表并说出类似的内容
if (HttpContext.Current.User.Identity.Name == // a user from the web.config list
这是可能的,如果是这样,你可以帮助我...谢谢
您可以从web.config获取授权规则,如下所示:
AuthorizationSection configSection =
(AuthorizationSection)ConfigurationManager.GetSection("system.web/authorization");
var users = new List<string>();
var rules = configSection.Rules;
foreach (AuthorizationRule rule in rules)
{
if (rule.Action == AuthorizationRuleAction.Allow)
{
foreach (string user in rule.Users)
{
if (!users.Contains(user)) users.Add(user);
}
}
}
但是你也必须注意规则的优先顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.