我需要做一些相当简单的事情:在我的ASP.NET MVC应用程序中,我想设置一个自定义IIdentity/IPrincipal.哪个更容易/更合适.我想要扩展默认值,以便我可以调用类似User.Identity.Id
和User.Identity.Role
.没什么特别的,只是一些额外的属性.
我已经阅读了大量的文章和问题,但我觉得我做得比实际更难.我觉得这很容易.如果用户登录,我想设置自定义IIdentity.所以我想,我将Application_PostAuthenticateRequest
在我的global.asax中实现.但是,每次请求都会调用它,并且我不希望在每个请求上调用数据库,这些请求将从数据库请求所有数据并放入自定义IPrincipal对象.这似乎也是非常不必要,缓慢,并且在错误的地方(在那里进行数据库调用)但我可能是错的.或者数据来自何处?
所以我想,每当用户登录时,我都可以在我的会话中添加一些必要的变量,我将其添加到Application_PostAuthenticateRequest
事件处理程序中的自定义IIdentity中.但是,我Context.Session
在null
那里,所以这也不是要走的路.
我已经在这一天工作了一天,我觉得我错过了什么.这不应该太难,对吧?我也对此附带的所有(半)相关内容感到困惑.MembershipProvider
,MembershipUser
,RoleProvider
,ProfileProvider
,IPrincipal
,IIdentity
,FormsAuthentication
....我是唯一一个谁发现这一切非常混乱?
如果有人能告诉我一个简单,优雅,高效的解决方案,可以在IIdentity上存储一些额外的数据而不需要额外的模糊...这将是非常棒的!我知道在SO上有类似的问题,但如果我需要的答案就在那里,我一定会忽略.
asp.net asp.net-mvc forms-authentication iprincipal iidentity
那么,什么是两个存在的目的IIdentity
和IPrincipal
,而不是一些IIdentityMergedWithPrincipal
?什么时候在同一个类中实现它们是不够的?
另外,为了理解目的,我想知道这个概念来自哪里:
System.Security.Principal
在这些接口中实现因此,不UserPrincipal
从System.DirectoryServices
的行为类似IPrincipal
,但不是偶然的还是打算实现它?
PS我正在寻找理念背后的推理,而不是利益/争议比较,所以请尽量不要开始基于意见的讨论
嗨,我使用自定义MembershipProvider.
我想知道应用程序场景中的当前用户名,但是当我尝试访问HttpContext.Current.User.Identity.Name时,它总是返回string.Empty.
if (Membership.ValidateUser(tbUsername.Text, tbPassword.Text))
{
FormsAuthentication.SetAuthCookie(tbUsername.Text, true);
bool x = User.Identity.IsAuthenticated; //true
string y = User.Identity.Name; //""
FormsAuthentication.RedirectFromLoginPage(tbUsername.Text, cbRememberMe.Checked);
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
在C#中,如何设置线程的标识?
例如,如果我已经启动了Thread MyThread,我可以更改MyThread的身份吗?
或者这不可能吗?
创建我自己的IPrincipal
,并IIdentity
实现如下图所示:
[ComVisible(true)]
[Serializable]
public sealed class CustomIdentity : IIdentity {
private readonly string _name;
private readonly string _email;
// and other stuffs
public CustomIdentity(string name) {
_name = name.Trim();
if(string.IsNullOrWhiteSpace(name))
return;
_email = (connect to database and read email and other stuffs);
}
public string Name {
get { return _name; }
}
public string Email {
get { return _email; }
}
public string AuthenticationType {
get { return "CustomIdentity"; }
}
public bool IsAuthenticated …
Run Code Online (Sandbox Code Playgroud) forms-authentication iprincipal iidentity custom-authentication asp.net-mvc-3
我有一个基本的MVC 2 beta应用程序,我正在尝试实现自定义Identity和Principal类.
我创建了实现IIdentity和IPrincipal接口的类,实例化它们,然后将CustomPrincipal对象分配给Global.asax的Application_AuthenticateRequest中的Context.User.
这一切都成功,对象看起来很好.当我开始渲染视图时,页面现在失败了.第一个失败是在以下代码行的默认LogoOnUserControl视图中:
[ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ]
Run Code Online (Sandbox Code Playgroud)
如果我把它拉出来,那么就会失败一个不同的"Html.ActionLink"代码行.
我收到的错误是:
WebDev.WebHost40.dll中出现"System.Runtime.Serialization.SerializationException"类型的异常,但未在用户代码中处理
附加信息:成员'Model.Entities.UserIdentity,Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'的类型未解析.
为了在MVC中使用自定义标识,我是否需要在Identity中实现一些其他属性?我试图在Identity类中实现[Serializable()]但它似乎没有影响.
更新: 我尝试了3-4种实现此方法的替代方法但仍然失败并出现相同的错误.如果我直接使用GenericIdentity/GenericPrincipal类,则不会出错.
GenericIdentity ident = new GenericIdentity("jzxcvcx");
GenericPrincipal princ = new GenericPrincipal(ident, null);
Context.User = princ;
Run Code Online (Sandbox Code Playgroud)
但是这让我无处可去,因为我试图使用CustomIdentity来保存一些属性.如果我实现了IIdentity/IPrincipal接口或继承了我的CustomIdentity/CustomPrincipal的GenericIdentity/GenericPrincipal,它将失败并显示上面的原始错误.
我有一个自定义类(具有UserID,UserName,UserEmail等属性)实现IIdentity.我通过自定义逻辑登录,该逻辑从sql读取.IIdentity的AuthenticationType应该返回什么?
什么Thread.CurrentPrincipal
用于?它如何帮助应用程序的身份验证和授权?是否有任何文章或资源可以帮助解释它的作用?
我尝试在现有数据库的基础上实现ASP.NET身份验证和授权.我们有一个网站调用webservice来获取其数据.要使用Web服务,我需要提供用户名和密码.知道这一点,我决定实施IIdentity和IPrincipal来存储加密的密码,并能够在执行webservice调用时提供它.在将来,我们可能希望使用更多asp.net的内置安全性,因此我实现了成员资格和角色提供程序并覆盖了我需要的内容(ValidateUser和GetRoles)尽管在验证用户之后感谢会员提供商实现我仍然将自己的CustomIdentity设置为Context.User,以便能够在需要时检索其密码.
只要允许用户访问该页面,它就能正常工作.但是当用户被拒绝时,框架会在我的CustomIdentity上抛出序列化异常,而不是抛出AccessDeniedException.我发现了一个非常相似的行为,此链接中描述了更多细节,但没有回复.
我的例外情况与上面的链接完全相同
Type is not resolved for member'CW.CustomAuthentication.CWIdentity,CW.CustomAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.Serialization.SerializationException: Type is not resolved for member 'CW.CustomAuthentication.CWIdentity,CW.CustomAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of …
Run Code Online (Sandbox Code Playgroud) serialization asp.net-membership iprincipal access-denied iidentity
使用WIF(Windows Identity Foundation)4.5,Microsoft创建了WindowsPrincipal
类,这是一种类型ClaimsPrincipal
.当然,这些类不可移植,但它们背后的接口是(IPrincipal
).ClaimsIndentity
实现IIdentity
接口的类也是如此.
我遇到的问题是这些类和WIF一般完全基于"声明"的概念,这很棒......但是这两个接口IPrincipal
并IIdentity
没有.不仅如此,ClaimsPrincipal
该类还具有一系列标识,而不仅仅是与之关联的单个标识.
Identity
和IsInRole
成员.AuthenticationType
,IsAuthenticated
和Name
成员.鉴于可移植类库只能访问这两个接口,如何获得实际的声明?
此外,在极少数情况下,委托人具有多重身份,如何获得"非主要"身份?
iidentity ×10
iprincipal ×7
.net ×3
asp.net ×3
c# ×2
.net-4.5 ×1
asp.net-mvc ×1
c#-4.0 ×1
httpcontext ×1
login ×1
security ×1
wif ×1