标签: iidentity

ASP.NET MVC - 设置自定义IIdentity或IPrincipal

我需要做一些相当简单的事情:在我的ASP.NET MVC应用程序中,我想设置一个自定义IIdentity/IPrincipal.哪个更容易/更合适.我想要扩展默认值,以便我可以调用类似User.Identity.IdUser.Identity.Role.没什么特别的,只是一些额外的属性.

我已经阅读了大量的文章和问题,但我觉得我做得比实际更难.我觉得这很容易.如果用户登录,我想设置自定义IIdentity.所以我想,我将Application_PostAuthenticateRequest在我的global.asax中实现.但是,每次请求都会调用它,并且我不希望在每个请求上调用数据库,这些请求将从数据库请求所有数据并放入自定义IPrincipal对象.这似乎也是非常不必要,缓慢,并且在错误的地方(在那里进行数据库调用)但我可能是错的.或者数据来自何处?

所以我想,每当用户登录时,我都可以在我的会话中添加一些必要的变量,我将其添加到Application_PostAuthenticateRequest事件处理程序中的自定义IIdentity中.但是,我Context.Sessionnull那里,所以这也不是要走的路.

我已经在这一天工作了一天,我觉得我错过了什么.这不应该太难,对吧?我也对此附带的所有(半)相关内容感到困惑.MembershipProvider,MembershipUser,RoleProvider,ProfileProvider,IPrincipal,IIdentity,FormsAuthentication....我是唯一一个谁发现这一切非常混乱?

如果有人能告诉我一个简单,优雅,高效的解决方案,可以在IIdentity上存储一些额外的数据而不需要额外的模糊...这将是非常棒的!我知道在SO上有类似的问题,但如果我需要的答案就在那里,我一定会忽略.

asp.net asp.net-mvc forms-authentication iprincipal iidentity

638
推荐指数
5
解决办法
21万
查看次数

在.NET中IIdentity和IPrincipal背后的想法是什么?

那么,什么是两个存在的目的IIdentityIPrincipal,而不是一些IIdentityMergedWithPrincipal?什么时候在同一个类中实现它们是不够的?

另外,为了理解目的,我想知道这个概念来自哪里:

  • 它起源于.Net
  • Identity/Principal的概念是设计模式,System.Security.Principal在这些接口中实现
  • 它起源于其他地方并支持兼容性

因此,不UserPrincipalSystem.DirectoryServices的行为类似IPrincipal,但不是偶然的还是打算实现它?

PS我正在寻找理念背后的推理,而不是利益/争议比较,所以请尽量不要开始基于意见的讨论

.net c# iprincipal iidentity

44
推荐指数
3
解决办法
2万
查看次数

HttpContext.Current.User.Identity.Name始终是string.Empty

嗨,我使用自定义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)

我错过了什么吗?

asp.net httpcontext iidentity custom-membershipprovider

26
推荐指数
3
解决办法
8万
查看次数

设置线程标识

在C#中,如何设置线程的标识?

例如,如果我已经启动了Thread MyThread,我可以更改MyThread的身份吗?

或者这不可能吗?

.net c# multithreading iidentity

24
推荐指数
1
解决办法
3万
查看次数

在MVC3中使用自定义IPrincipal和IIdentity

创建我自己的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

21
推荐指数
1
解决办法
1万
查看次数

在MVC中实现自定义标识和IPrincipal

我有一个基本的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,它将失败并显示上面的原始错误.

iprincipal iidentity asp.net-mvc-2

19
推荐指数
2
解决办法
1万
查看次数

实现IIdentity,什么是AuthenticationType?

我有一个自定义类(具有UserID,UserName,UserEmail等属性)实现IIdentity.我通过自定义逻辑登录,该逻辑从sql读取.IIdentity的AuthenticationType应该返回什么?

.net authentication login iidentity

13
推荐指数
1
解决办法
3201
查看次数

什么是Thread.CurrentPrincipal,它有什么作用?

什么Thread.CurrentPrincipal用于?它如何帮助应用程序的身份验证和授权?是否有任何文章或资源可以帮助解释它的作用?

asp.net security iprincipal iidentity c#-4.0

13
推荐指数
1
解决办法
7176
查看次数

在ASP.NET中拒绝用户时,'CustomIdentity'上的SerializationException

我尝试在现有数据库的基础上实现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

8
推荐指数
2
解决办法
9238
查看次数

你如何在便携式类库中使用IPrincipal和IIdentity?

使用WIF(Windows Identity Foundation)4.5,Microsoft创建了WindowsPrincipal类,这是一种类型ClaimsPrincipal.当然,这些类不可移植,但它们背后的接口是(IPrincipal).ClaimsIndentity实现IIdentity接口的类也是如此.

我遇到的问题是这些类和WIF一般完全基于"声明"的概念,这很棒......但是这两个接口IPrincipalIIdentity没有.不仅如此,ClaimsPrincipal该类还具有一系列标识,而不仅仅是与之关联的单个标识.

  • IPrincipal有IdentityIsInRole成员.
  • IIdentity的有AuthenticationType,IsAuthenticatedName成员.

鉴于可移植类库只能访问这两个接口,如何获得实际的声明?

此外,在极少数情况下,委托人具有多重身份,如何获得"非主要"身份?

iprincipal iidentity wif portable-class-library .net-4.5

6
推荐指数
1
解决办法
2520
查看次数