5

I'm crafting a custom Owin Middleware to handle logging into .NET web apps using our custom token server.

I've got everything working. The token is parsed into claims for an identity, the identity is put inside of an AuthenticationTicket, and the user is successfully logged in using CookieAuthentication.

I'd like to make querying the claims easier for other developers, so I inherited from ClaimsIdentity. I create an instance of my class and pass it along to the AuthenticationTicket constructor.

My problem is that User.Identity in my controller action is not an instance of my class.

What am I missing? Is something in ASP.NET recreating my Identity instance and simply copying over all of the claims?

3
  • 2
    The Identity gets recreated on every request. For simplifying claim queries, what I typically do is just create an extension method on IPrincipal unless I have further needs that require a custom identity.
    – steve v
    Commented Sep 4, 2015 at 18:13
  • That makes perfect sense. The cookie storing the claims has no idea what class I used to create it. Why did you leave a comment instead of answering?
    – Spivonious
    Commented Sep 4, 2015 at 20:17
  • 2
    Because it is in fact possible to recreate your custom identity each time by hooking into the application's global events. The answer is "yes, it is possible" but I don't have the code handy to demonstrate how to do it. I was merely noting that it is getting recreated to answer your last question. Instead I suggested an alternative that you would have to research to implement (extension method).
    – steve v
    Commented Sep 4, 2015 at 20:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.