Identity 2.0: creating a custom ClaimsIdentity, for example: User.Identity.GetUserById <int> (int id) to validate the request

See this similar question: Need access to more custom properties in User.Identity

I would like to create my own authentication methods for use with my Razor views, which make it easy to use the IdentityUser properties associated with the User.Identity object, but I'm not sure how to do this. I want to create several custom extensions similar to User.Identity.GetUserName() , User.Identity.GetUserById() , etc., Instead of using this ViewContextExtension method. My authentication type is currently the default type DefaultAuthenticationTypes.ApplicationCookie from VS2013 MVC5 template. As stated in the ad, I need this claim to be inserted after the user logged in.

My questions:

How and where do you create a custom application with out this IIdentity in the IPrincipal section?

This will allow me to access the properties of the User through CookieAuthentication in the view for objects in the DDD setting, where I have several DbContexts in one application using Identity 2.0. Ultimately, I will use WebAPI, but now I want to keep it as simple as possible. I found this SO Q & A , but it focuses on web forms using Tickets. Not sure if the difference between tickets and tokens?

This is the current approach that uses ViewContext from the base controller:

View:

  @using Microsoft.AspNet.Identity @using Globals.Helpers @using Identity //custom Identity for Domain @using Microsoft.AspNet.Identity.Owin @if (Request.IsAuthenticated) { var url = @ViewContext.BaseController().GetAvatarUrlById(User.Identity.GetUserId<int>()); //... } 

Basecontroller.cs

  public string GetAvatarUrlById(int id) { var user = UserManager.FindById(id); return "../../" + user.ImageUrl; } 

Extensions.cs

  public static class ViewContextExtension { public static BaseController BaseController(this ViewContext view) { var baseController = (BaseController)view.Controller; return baseController; } } 

What am I looking for, but where and how?

View:

 <img src="@User.Identity.GetAvatarUrl()" alt="User.Identity.GetAvatarUrl()" /> 

Decision

I just edited the Extension.cs file and used inheritance for the base controller, which is used for _LoginPartial.cshtml and edited the ViewContextExtension class:

 #region ViewContextExt public static class ViewContextExtension { public static BaseController BaseController(this ViewContext view) { var baseController = (BaseController)view.Controller; return baseController; } public static string GetAvatarUrl(this IIdentity identity) { return ((ClaimsIdentity)identity).Claims.First(c => c.Type == "AvatarUrl").Value; } } 

}

endregion

+3
c # asp.net-mvc-5 asp.net-identity-2 razor-3
Dec 29 '15 at 3:53 on
source share
2 answers

The IIdentity in MVC will be a issued token that matches the user ID. This is different from any object or method that you use on the internal server that represents the user (for example, the User class). If you want to use the user ID to get the user value, you need to put it in your application object (i.e. the identification token) when they log in (or at some other point in time).

You can add a request at any time, indicating the user identity.

 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); identity.AddClaim(new Claim("PhoneNumber", "123-456-7890")); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); 

When you have this requirement inserted into their token, you can get it with an extension method like this ...

 public static string GetPhoneNumber(this IIdentity identity) { return ((ClaimsIdentity)identity).FindFirstValue("PhoneNumber"); } 

Razor

 @using MyProject.Web.Extensions <img src="@User.Identity.GetPhoneNumber()" /> 
+9
Dec 29 '15 at 19:08
source share

I really found a solution using the answer to this question SO from LukeP , but as footwear notes, this is a preliminary MVC5, and we could just add to the application instead,

I made the following changes:

  interface IDomainPrincipal : IPrincipal { int Id { get; set; } string UserName { get; set; } string AvatarUrl { get; set; } } public class DomainPrincipal : IDomainPrincipal { public IIdentity Identity { get; private set; } public bool IsInRole(string role) { return false; } public DomainPrincipal(string email) { this.Identity = new GenericIdentity(email); } public int Id { get; set; } public string UserName { get; set; } public string AvatarUrl { get; set; } } 

Then I used @User.Id, @User.UserName, @User.AvatarUrl in my @Razor Views respectively

0
Jan 15 '15 at 2:03
source share



All Articles