帮助会员 [英] Help with Membership

查看:55
本文介绍了帮助会员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个项目上待了一个多月了.该应用程序是根据用户和角色框架进行设计的.我想始终检查登录的用户是否是管理员,如果为true(用户为管理员),将显示admin菜单,否则菜单的visible属性为false.我在应用程序的每个页面上实现了此检查,是否有任何方法可以编写一次代码,并在应用程序中的每个其他ASP.NET页面的page_Load()事件上调用它.这是代码吗?

I have been on this project for quite a month now. the application is design based on users and role framework. I want to always check if the user logging in is an administrator, if true (the user is administrator) an admin menu will be displayed else the visible property of the menu is false. I implemented this check on every page in the application, Is there any way to write the code once and call it on page_Load() event of every other ASP.NET Page in the application. Here is the code?

private void CheckIfUserIsAdmin()
    {
        if (Request.IsAuthenticated)
        {
            try
            {
                string username = User.Identity.Name;
                if (username == "admin" && User.IsInRole("Administrator"))
                {
                    Panel PanelAdmin = (Panel)Master.FindControl("PanelAdminMenu");
                    PanelAdmin.Visible = true;
                }
            }
            catch (NullReferenceException nullex)
            {
                nullex.Message.ToString();
            }
        }
    }

推荐答案

是.

0)创建一个从System.Web.UI.Page类继承的基类

1)将您的代码放入新的基类中
2)在所有页面中继承新的基类(而不是Web.UI.Page).
Yes.

0) Create a base class that inherits from the System.Web.UI.Page class

1) Put your code in the new base class

2) Inherit the new base class (instead of Web.UI.Page) in all of your pages.


当然,有一种方法可以编写代码并使它可被多个类使用,这称为遗产.您是否在页面或您使用的每个页面中都写入了请求"或用户"属性?当然不是,您的页面是从System.Web.UI.Page派生的.

创建一个从System.Web.UI.Page派生的类,将此代码放在其中,并从该类派生您现有的页面.
Of course there is a way to write code and have it usable by multiple class, its called inheritance. Did you write the Request or User properties in your page, or each page you use? Of course not, your page is derived from System.Web.UI.Page.

Create a class that derives from System.Web.UI.Page, place this code in it and derive your existing pages from this class.


这篇关于帮助会员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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