asp.net-mvc-2 – 在MVC中实现自定义标识和IPrincipal
|
我有一个基本的MVC 2 beta应用程序,我试图实现一个自定义的身份和Principal类。 我创建了实现IIdentity和IPrincipal接口的类,实例化它们,然后将CustomPrincipal对象分配给Global.asax的Application_AuthenticateRequest中的Context.User。 这一切都成功,对象看起来不错。当我开始渲染视图时,页面现在失败。第一个失败是在以下代码行的默认LogoOnUserControl视图中: [ <%= Html.ActionLink("Log Off","LogOff","Account") %> ]
如果我把它拉出来,然后在不同的“Html.ActionLink”代码行失败。 我收到的错误是:
在我的身份中需要实现一些额外的属性,以便在MVC中使用自定义标识?我试图在Identity类中实现[Serializable()],但它似乎没有影响。 更新: GenericIdentity ident = new GenericIdentity("jzxcvcx");
GenericPrincipal princ = new GenericPrincipal(ident,null);
Context.User = princ;
但是,这让我无处可寻,因为我试图使用CustomIdentity来保存几个属性。如果我实现了IIdentity / IPrincipal接口,或者为我的CustomIdentity / CustomPrincipal继承了GenericIdentity / GenericPrincipal,那么它将失败,并出现上面的原始错误。 解决方法我从网络中得到了一些帮助,我想到了这一点:)诀窍是你必须在实现IIdentity的类中实现ISerializable接口。我希望这有助于保存别人一些时间:)班级申报: [Serializable]
public class ForumUserIdentity : IIdentity,ISerializable
实现ISerializable: #region ISerializable Members
public void GetObjectData(SerializationInfo info,StreamingContext context)
{
if (context.State == StreamingContextStates.CrossAppDomain)
{
GenericIdentity gIdent = new GenericIdentity(this.Name,this.AuthenticationType);
info.SetType(gIdent.GetType());
System.Reflection.MemberInfo[] serializableMembers;
object[] serializableValues;
serializableMembers = FormatterServices.GetSerializableMembers(gIdent.GetType());
serializableValues = FormatterServices.GetObjectData(gIdent,serializableMembers);
for (int i = 0; i < serializableMembers.Length; i++)
{
info.AddValue(serializableMembers[i].Name,serializableValues[i]);
}
}
else
{
throw new InvalidOperationException("Serialization not supported");
}
}
#endregion
这是link to the article that has more detail on the “Feature” (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 估算Web应用程序峰值带宽需求的最准确方法是什么
- asp.net-mvc – ASP.NET MVC是否使用常规工具箱控件?
- asp.net-mvc – 为一个MVC视图使用两个强类型模型
- asp.net-mvc – MVC DateTime验证失败
- asp.net-mvc-3 – 方法“OrderBy”必须在方法“跳过”异常之
- asp.net – 按钮可以验证更多验证组吗?
- asp.net – 为什么在FireFox中禁用时链接按钮不会变灰?
- asp.net – IIS 7.0和IIS 7.5之间有什么区别?
- asp.net-mvc – 控制器操作无法从JSON读取Guid POST
- asp.net-mvc-4 – 为什么我们需要MVC中的Web API? mvc中re
- asp.net-mvc – ASP.NET MVC – 什么是UrlRoutin
- asp.net – global.asax断点未命中
- asp.net-mvc-3 – html.dropdownlist MVC3混乱
- asp.net-mvc-4 – 如何在服务器上安装ASP.NET MV
- asp.net-mvc – ASP.NET MVC – Respository / S
- asp.net-mvc – 如何使用wmd-editor控件检索mark
- asp.net-mvc-3 – 从JsonResult MVC3 / Razor中动
- 我如何在ASP.NET MVC中使用Application_Error?
- 在ASP.NET Core中相关的ConfigureAwait(false)?
- asp.net-mvc – 具有持久HTTP连接的IDbConnectio
