asp.net我如何引用授权的用户很难codeD在web.config中我的code [英] asp.net How do I reference authorized users hard coded in web.config in my code

查看:99
本文介绍了asp.net我如何引用授权的用户很难codeD在web.config中我的code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立Intranet上的一个网站和一个目录中只能通过硬codeD授权的用户访问。他们在web.config中定义的。它看起来与此类似。

I am building a website on an intranet and one of the directories can only be accessed by hard coded authorized users. They are defined in web.config. It looks similar to this.

<location path="admin">
    <system.web>
        <authorization>
            <allow users="user1"/>
            <allow users="user2"/>
            <allow users="user3"/>
            <allow users="user4"/>
            <deny users="*"/>
        </authorization>
    </system.web>
</location> 

我想接着就是创建一个链接到这个目录中只出现那些用户...
目前,建立链接,我复查有窗户的用户名和努力再次编码它们像这样...

What I want then is to create a link to this directory which only appears to those users... At the moment, to build the link I'm rechecking there windows usernames and hard coding them in again like this...

<% 
    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文件中引用我的名单,并说像

But what I want to do is reference my list from the webiconfig file and say something like

if (HttpContext.Current.User.Identity.Name == // a user from the web.config list

这是可能的,如果是这样,你可以帮我...谢谢

Is this possible and if so can you help me... Thanks

推荐答案

您可以从web.config中的授权规则是这样的:

You can get the authorization rules from web.config like this:

            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);
                }
            }
        }

但你也必须支付atention到规则的precedence。

But you must also pay atention to the precedence of the rules.

这篇关于asp.net我如何引用授权的用户很难codeD在web.config中我的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆