使用MVC5和OWIN的自定义身份

黑冰

我试图使用MVC5和OWIN身份验证将自定义属性添加到网站的ApplicationUser中。我已经阅读了https://stackoverflow.com/a/10524305/264607,我喜欢它如何与基本控制器集成在一起以方便地访问新属性。我的问题是,当我将HTTPContext.Current.User属性设置为新的IPrincipal时,出现空引用错误:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.Security.UrlAuthorizationModule.OnEnter(Object source, EventArgs eventArgs) +127
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

这是我的代码:

    protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

            ApplicationUser user = userManager.FindByName(HttpContext.Current.User.Identity.Name);

            PatientPortalPrincipal newUser = new PatientPortalPrincipal();
            newUser.BirthDate = user.BirthDate;
            newUser.InvitationCode = user.InvitationCode;
            newUser.PatientNumber = user.PatientNumber;

            //Claim cPatient = new Claim(typeof(PatientPortalPrincipal).ToString(), );

            HttpContext.Current.User = newUser;
        }
    }

public class PatientPortalPrincipal : ClaimsPrincipal, IPatientPortalPrincipal
{
    public PatientPortalPrincipal(ApplicationUser user)
    {
        Identity = new GenericIdentity(user.UserName);
        BirthDate = user.BirthDate;
        InvitationCode = user.InvitationCode;
    }

    public PatientPortalPrincipal() { }

    public new bool IsInRole(string role)
    {
        if(!string.IsNullOrWhiteSpace(role))
            return Role.ToString().Equals(role);

        return false;
    }

    public new IIdentity Identity { get; private set; }
    public WindowsBuiltInRole Role { get; set; }
    public DateTime BirthDate { get; set; }
    public string InvitationCode { get; set; }
    public string PatientNumber { get; set; }
}

public interface IPatientPortalPrincipal : IPrincipal
{

    WindowsBuiltInRole Role { get; set; }
    DateTime BirthDate { get; set; }
    string InvitationCode { get; set; }
    string PatientNumber { get; set; }
}

关于如何执行此操作的方法方面,我还没有发现太多,我已经阅读了以下文章:

http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx

第二个链接中的注释指示我可能使用了声明(http://msdn.microsoft.com/en-us/library/ms734687.aspx?cs-save-lang=1&cs-lang=csharp),但该文章已链接to不会显示如何将它们添加到IPrincipalHttpContext.Current.User是),或者应该在管道中的何处将它们添加到ClaimsIdentity(是is的具体类User)。我倾向于使用声明,但是我需要知道将这些新声明添加到用户的位置。

即使可以使用声明,但我对自己的自定义IPrincipal做错了事感到好奇,因为我似乎已经实现了它所要求的一切。

黑冰

我可以使用Claims基于安全性的方法来使某些事情起作用,因此,如果您希望快速完成某件事,那么这就是我目前所拥有的:

AccountController(mine在SignInAsync方法的登录过程中,向由创建的标识添加新的声明UserManager

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    identity.AddClaim(new Claim("PatientNumber", user.PatientNumber)); //This is what I added
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

然后,在我的基本控制器类中,我仅添加了一个属性:

private string _patientNumber;
public string PatientNumber
{
    get
    {
        if (string.IsNullOrWhiteSpace(_patientNumber))
        {
            try
            {
                var cp = ClaimsPrincipal.Current.Identities.First();
                var patientNumber = cp.Claims.First(c => c.Type == "PatientNumber").Value;
                _patientNumber = patientNumber;
            }
            catch (Exception)
            {
            }
        }
        return _patientNumber;
    }
}

该链接对于理赔知识很有帮助:http : //msdn.microsoft.com/zh-cn/library/ms734687.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1


有关IPrincipal的问题的更新

我将其追溯到该Identity物业。问题是我PatientPortalPrincipal在没有设置Identity属性上提供了默认构造函数我最终要做的是删除默认构造函数,并从内部调用正确的构造函数Application_PostAuthenticateRequest,更新后的代码如下

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

        ApplicationUser user = userManager.FindByName(HttpContext.Current.User.Identity.Name);

        PatientPortalPrincipal newUser = new PatientPortalPrincipal(user);
        newUser.BirthDate = user.BirthDate;
        newUser.InvitationCode = user.InvitationCode;
        newUser.PatientNumber = user.PatientNumber;

        //Claim cPatient = new Claim(typeof(PatientPortalPrincipal).ToString(), );

        HttpContext.Current.User = newUser;
    }
}

这使整个工作正常!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Windows身份验证和OWIN的ASP.NET MVC5 / AngularJS / Web API应用

来自分类Dev

如何为新的Microsoft.Asp.NET身份(MVC5)设置自定义架构

来自分类Dev

MVC5 ApplicationUser自定义属性

来自分类Dev

如何使用Windows身份验证和自定义角色提供程序设置OWIN

来自分类Dev

如何使用VS2013 MVC5模板显示自定义配置文件信息?

来自分类Dev

MVC 5在web.config中自定义OWIN身份验证选项

来自分类Dev

如何在ASP.NET MVC5中创建自定义支架模板?

来自分类Dev

使用MVC5和OWIN的自定义身份

来自分类Dev

自定义MVC5 ASP.NET Identity中的cookie值

来自分类Dev

使用自定义表扩展ASP.NET MVC 5身份

来自分类Dev

MVC5中的自定义验证错误消息

来自分类Dev

使用托管在GoDaddy上的ASP.NET MVC5 OWIN身份验证在5分钟后过期

来自分类Dev

自定义ASP.NET身份,OWIN和社交提供程序登录

来自分类Dev

MVC5自定义路由从区域中删除控制器名称

来自分类Dev

OWIN身份验证和自定义响应

来自分类Dev

ASP.NET MVC5以自定义格式显示日期

来自分类Dev

如何在MVC5中使用自定义属性路由重定向到路由

来自分类Dev

在Mvc5中将Html.TextBox与自定义属性一起使用

来自分类Dev

如何使用StructureMap(4.0)将依赖项注入自定义属性?MVC5项目

来自分类Dev

在ASP.NET MVC5中使用自定义登录进行表单身份验证

来自分类Dev

MVC5中的自定义路由和名称空间

来自分类Dev

使用托管在GoDaddy上的ASP.NET MVC5 OWIN身份验证在5分钟后过期

来自分类Dev

自定义JQuery捆绑包,MVC5

来自分类Dev

如何使用MVC5自定义验证集合

来自分类Dev

MVC5中的自定义路由

来自分类Dev

使用WCF的MVC5和WebAPI自定义路由

来自分类Dev

在ASP.NET MVC5中使用自定义登录进行表单身份验证

来自分类Dev

使用MVC进行自定义身份验证

来自分类Dev

使用mvc5身份自定义和集成自己的数据库

Related 相关文章

  1. 1

    使用Windows身份验证和OWIN的ASP.NET MVC5 / AngularJS / Web API应用

  2. 2

    如何为新的Microsoft.Asp.NET身份(MVC5)设置自定义架构

  3. 3

    MVC5 ApplicationUser自定义属性

  4. 4

    如何使用Windows身份验证和自定义角色提供程序设置OWIN

  5. 5

    如何使用VS2013 MVC5模板显示自定义配置文件信息?

  6. 6

    MVC 5在web.config中自定义OWIN身份验证选项

  7. 7

    如何在ASP.NET MVC5中创建自定义支架模板?

  8. 8

    使用MVC5和OWIN的自定义身份

  9. 9

    自定义MVC5 ASP.NET Identity中的cookie值

  10. 10

    使用自定义表扩展ASP.NET MVC 5身份

  11. 11

    MVC5中的自定义验证错误消息

  12. 12

    使用托管在GoDaddy上的ASP.NET MVC5 OWIN身份验证在5分钟后过期

  13. 13

    自定义ASP.NET身份,OWIN和社交提供程序登录

  14. 14

    MVC5自定义路由从区域中删除控制器名称

  15. 15

    OWIN身份验证和自定义响应

  16. 16

    ASP.NET MVC5以自定义格式显示日期

  17. 17

    如何在MVC5中使用自定义属性路由重定向到路由

  18. 18

    在Mvc5中将Html.TextBox与自定义属性一起使用

  19. 19

    如何使用StructureMap(4.0)将依赖项注入自定义属性?MVC5项目

  20. 20

    在ASP.NET MVC5中使用自定义登录进行表单身份验证

  21. 21

    MVC5中的自定义路由和名称空间

  22. 22

    使用托管在GoDaddy上的ASP.NET MVC5 OWIN身份验证在5分钟后过期

  23. 23

    自定义JQuery捆绑包,MVC5

  24. 24

    如何使用MVC5自定义验证集合

  25. 25

    MVC5中的自定义路由

  26. 26

    使用WCF的MVC5和WebAPI自定义路由

  27. 27

    在ASP.NET MVC5中使用自定义登录进行表单身份验证

  28. 28

    使用MVC进行自定义身份验证

  29. 29

    使用mvc5身份自定义和集成自己的数据库

热门标签

归档