关于Principal类型转换出错一问

yantaian 2004-04-05 06:29:34
程序出错的位置实现的是利用数据库的Roles表、Permission表实现角色权限机制的功能。不知道这条错误是什么意思,百思不得其解:(

请前辈们帮忙,谢谢~~

“/Per”应用程序中的服务器错误。
--------------------------------------------------------------------------

指定的转换无效。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 指定的转换无效。

源错误:


行 27:private void Page_Load(object sender, System.EventArgs e)
行 28:{
行 29: FreakPrincipal currentPrincipal = (FreakPrincipal)Context.User;
行 30: if (!currentPrincipal.HasPermission((int)AccountsPermissions.CreateRoles))
行 31: {


源文件: c:\inetpub\wwwroot\per\modules\secure\admin\roles.aspx.cs 行: 29

堆栈跟踪:

[InvalidCastException: 指定的转换无效。]
Freak.WebModules.Accounts.Web.Roles.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\per\modules\secure\admin\roles.aspx.cs:29
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
...全文
124 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yantaian 2004-04-05
  • 打赏
  • 举报
回复
啊!我把那两行代码放在OnInit里,程序真的就通过了~~~

太感谢您了!!!!
cuike519 2004-04-05
  • 打赏
  • 举报
回复
如果你继承了这个页面可能是你的Page_Load没有执行!你看看基类里面的Page_Load执行了没有。如果没有执行将这些代码放到OnInit里面应该可以解决你的问题!
cuike519 2004-04-05
  • 打赏
  • 举报
回复
你的这种方法是使用基类实例自己的FreakPrincipal用来基于角色的身份验证。
你的页面一定继承一个BasePage在BasePage的On_load里面将当前的User实例成自定义个FreakPrincipal对象,然后在子类中使用。
yantaian 2004-04-05
  • 打赏
  • 举报
回复
谢谢,可是我的基类里是有这种代码的啊!

private void FreakPage_Load(object sender, System.EventArgs e)
{
// TODO: Place any code that will take place BEFORE the Page_Load event
// in the regular page, e.g. cache management, authentication verification,
// etc.
if (Context.User.Identity.IsAuthenticated)
{
if (!(Context.User is FreakPrincipal))
{
// 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 email
// address (because we forced it to be as such in the login.aspx page)
FreakPrincipal newUser = new FreakPrincipal( Context.User.Identity.Name );
Context.User = newUser;
}
}
}

为什么不能转换?前辈们请指点我一下~ 谢谢~
cuike519 2004-04-05
  • 打赏
  • 举报
回复
FreakPrincipal newUser = new FreakPrincipal( Context.User.Identity.Name );
Context.User = newUser;

要在基类里面先执行上面的代码才能做正确的转换!
yantaian 2004-04-05
  • 打赏
  • 举报
回复
FreakPrincipal是继承自IPrincipal的自定义类,实现一些安全校验授权之类的功能:

using System;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.Text;

using Freak.WebModules.Accounts;

namespace Freak.WebModules.Accounts.Business
{
public class FreakPrincipal: Freak.WebModules.Business.BizObject, System.Security.Principal.IPrincipal
{
protected System.Security.Principal.IIdentity identity;
protected ArrayList permissionList;
protected ArrayList roleList;

public System.Security.Principal.IIdentity Identity
{
get
{
return identity;
}
set
{
identity = value;
}
}

public bool IsInRole(string role)
{
return roleList.Contains( role );
}

public bool HasPermission( int permissionId )
{
return permissionList.Contains( permissionId );
}

public ArrayList Roles
{
get
{
return roleList;
}
}

public ArrayList Permissions
{
get
{
return permissionList;
}
}

public FreakPrincipal( int userId )
{
Configuration.ModuleSettings moduleSettings = Configuration.ModuleConfig.GetSettings();
Data.User dataUser = new Data.User( moduleSettings.ConnectionString );

Identity = new SiteIdentity( userId );
permissionList = dataUser.GetEffectivePermissionList( userId );
roleList = dataUser.GetUserRoles( userId );

}

public FreakPrincipal( string email )
{
Configuration.ModuleSettings moduleSettings = Configuration.ModuleConfig.GetSettings();
Data.User dataUser = new Data.User( moduleSettings.ConnectionString );

Identity = new SiteIdentity( email );
permissionList = dataUser.GetEffectivePermissionList( ((Business.SiteIdentity)Identity).UserId );
roleList = dataUser.GetUserRoles( ((Business.SiteIdentity)Identity).UserId );

}

public static FreakPrincipal ValidateLogin(string email, string password)
{
Configuration.ModuleSettings moduleSettings = Configuration.ModuleConfig.GetSettings();
int newId;

byte[] cryptPassword = EncryptPassword( password );

Data.User dataUser = new Data.User( moduleSettings.ConnectionString );
if ( (newId = dataUser.ValidateLogin(email, cryptPassword)) > -1 )
return new FreakPrincipal( newId );
else
return null;
}

public static byte[] EncryptPassword(string password)
{
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] hashBytes = encoding.GetBytes( password );

// compute SHA-1 hash.
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] cryptPassword = sha1.ComputeHash ( hashBytes );

return cryptPassword;
}

}
}
luckyfool 2004-04-05
  • 打赏
  • 举报
回复
行 29: FreakPrincipal currentPrincipal = (FreakPrincipal)Context.User;
这行有错误,给出FreakPrincipal的定义先

62,253

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧