朋友们有的知道Wrox.ThePhile中的权限许可的设计吧,
我按照这个设计思想,自己也做了一个,可出现了以下的错误指定的转换无效。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误: 
WenyiPrincipal currUser = (WenyiPrincipal)Context.User;//就是这里出错
Response.Write(currUser.MyIdentity.EmailAddress);而我的WenyiPrincipal是从System.Security.Principal.IPrincipal派生的
对Context.User的赋值在用户登陆后:
WenyiPrincipal newUser=new WenyiPrincipal(name);
                    Context.User = newUser;

解决方案 »

  1.   

    先判断
    Context.User==null
    吗?
      

  2.   

    先判断
    Context.User==null
    吗?
      

  3.   

    和你的Web.config 配置有关系
    参考
    http://www.microsoft.com/china/technet/security/guidance/secmod38.mspx
      

  4.   

    Context.User在用户登陆后肯定是不为空,我可以通过Context.User.Identity.Name来取出用户名
      

  5.   

    >>>对Context.User的赋值在用户登陆后检测一下Context.User里的类型Response.Write(Context.User.GetType().Name);如果你设置自定义用户功能,你需要对每次请求在Application_AuthenticateRequest里设置,参考Role-based Security with Forms Authentication
    http://www.codeproject.com/aspnet/formsroleauth.asp
      

  6.   

    Context.User的类型是GenericPrincipal我现在是要把Context.User读到WenyiPrincipal类型里啊,怎么办呢?
    登陆时我是对Context.User赋值为一个WenyiPrincipal类型的对象啊
      

  7.   

    先要有:Context.User = new SitePrincipal();然后在你的global.asax.cs中:
    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
    。。
    CustomPrincipal newUser = new CustomPrincipal(identity, roles);
    Context.User = newUser;
    }
      

  8.   

    我的new WenyiPrincipal(name)有一个name的参数,在global.asax.cs中怎么传进来?
      

  9.   

    Wrox.ThePhile 那里有个叫philepage的基类里面的pageload事件里面就定义了 If Context.User.Identity.IsAuthenticated Then        If Not (TypeOf context.User Is SitePrincipal) Then          ' ASP.NET's regular forms authentication picked up our cookie, but
              ' we()  haven't replaced the default context user with our own.
              ' Let's do that now. We know that the previous context.user.
              ' identity.name is the e-mail address (because we forced it to be
              ' as such in the login.aspx page)          Dim newUser As New SitePrincipal(Context.User.Identity.Name)
              Context.User = newUser跟上面的老大们说的在global.asax改是一个原理。因为phile定义了个基类所以不需要在global.asax改了。不过我自己的问题来了,我要siteprincipal 传递两个变量怎么办阿?只有一个name属性....
      

  10.   

    rewrite the SitePrincipal class' constructors to take two parameters or add an additional property...
      

  11.   

    Eddie005(暴走005) (五个三角 ) 信誉:100  2004-08-31  2004-08-31???
      

  12.   

    cmd.CommandText = "select M_Name from Members where Member_id="+Context.User.Identity.Name;
      

  13.   

    登陆后还要在
    Application_AuthenticateRequest
    在一次将你自定义的Principal类
    赋值给他
    这样你就可以在任何页面上都可以用你
    自己的自定义类了
      

  14.   

    我的SitePrincipal的构造函数本来就是传递两参数,后来发现Context.User.Identity.Name只能回传一个函数。结果被迫写了两个构造函数
      

  15.   

    俺是个初学者,也碰到过这个问题,搜出了这个帖子,也看了各位大老的回答,可一直没解决这个问题,我就想,为什么在书本所附的工程中运行是正常的呢?没办法,只好一行行校对了,呵呵,功夫不负木头人,原来是这样的,在wrox.thephile中有个叫PhilePage的页面基类。我们的Web页都从该页继承下来,生成的代码是这样的:
            override protected void OnInit(EventArgs e)
            {
                //
                // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                //
                InitializeComponent();
                base.OnInit(e);
            }
            
            /**//// <summary>
            /// 设计器支持所需的方法 - 不要使用代码编辑器修改
            /// 此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {    
                this.Load += new System.EventHandler(this.Page_Load);
            }
            #endregion注意这里是先调用InitializeComponent()后调用父类的base.OnInit(e),如果这样的话就在页面载入是先调用的是自己的Page_Load()方法,后调用父类的OnInit(e),这样PhilePage的Phile_Load()就形同虚设了。只要把它们的顺序换过来就OK了。