2016-05-30 22 views
3

.NET .NET RC2に.NET 4.6バージョンを移植しており、.NET Core RC2でのフォローを行う方法が不思議です。.NET Core RC2のサインインに関するクレーム

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
{ 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     userIdentity.AddClaim(new Claim("FullName", string.Format("{0} {1}", this.Firstname, this.Lastname))); 
     userIdentity.AddClaim(new Claim("Organization", this.Organization.Name)); 
     userIdentity.AddClaim(new Claim("Role", manager.GetRoles(this.Id).FirstOrDefault())); 
     userIdentity.AddClaim(new Claim("ProfileImage", this.ProfileImageUrl)); 
     // Add custom user claims here 
     return userIdentity; 
} 

、次にアイデンティティの拡張方法。私User.Identity.ProfileImg();などを使用した結果を与える

public static class IdentityExtensions 
{ 
    public static string FullName(this IIdentity identity) 
    { 
     var claim = ((ClaimsIdentity)identity).FindFirst("FullName"); 
     // Test for null to avoid issues during local testing 
     return (claim != null) ? claim.Value : string.Empty; 
    } 

    public static string Organization(this IIdentity identity) 
    { 
     var claim = ((ClaimsIdentity)identity).FindFirst("Organization"); 
     // Test for null to avoid issues during local testing 
     return (claim != null) ? claim.Value : string.Empty; 
    } 

    public static string Role(this IIdentity identity) 
    { 
     var claim = ((ClaimsIdentity)identity).FindFirst("Role"); 
     // Test for null to avoid issues during local testing 
     return (claim != null) ? claim.Value : string.Empty; 
    } 

    public static string ProfileImage(this IIdentity identity) 
    { 
     var claim = ((ClaimsIdentity)identity).FindFirst("ProfileImage"); 
     // Test for null to avoid issues during local testing 
     return (claim != null) ? claim.Value : string.Empty; 
    } 
} 

..あなたたちは待っせるため申し訳ありません

+0

のために私の見解では、このようなものです。 – conterio

+0

私は自分のコンピュータに戻ったときに私がそれをどのように解決したかについて答えを提供します。 @JeremyConterio – Rovdjuret

+0

それは素晴らしいだろう – conterio

答えて

4

ユーザー作成時に次のようにして解決しました。私の場合、ユーザーが作成されたときと同じように、ユーザーとの関係として保存されたクレームを作成します。 次に、これらの値はプロセス全体を通じて更新されます。つまり、誰かがクレームテーブルで更新する必要がある値を変更するたびに更新されます。

var user1 = new ApplicationUser() 
{ 
    Firstname = "MyName", 
    Lastname = "MyLastname", 
    UserName = "[email protected]", 
    Email = "[email protected]", 
    EmailConfirmed = true, 
    PhoneNumber = "000000000", 
    OrganizationId = organization.Id, 
    ProfileImageUrl = "user.jpg" 
}; 
await userManager.CreateAsync(user1, "Qwerty1!"); 
await userManager.AddToRoleAsync(user1, "SuperAdmin"); 

var claims1 = new List<Claim> { 
    new Claim("Email", user1.Email), 
    new Claim("FullName", string.Format("{0} {1}", user1.Firstname, user1.Lastname)), 
    new Claim("Organization", organization.Name), 
    new Claim("Role", "SuperAdmin"), 
    new Claim("ProfileImage", user1.ProfileImageUrl) 
}; 

await userManager.AddClaimsAsync(user1, claims1); 

最後に、私は拡張機能を作成して、現在のログインユーザーのビューとコントローラーにアクセスします。

using System.Security.Claims; 
using System.Security.Principal; 

namespace Core.Extensions 
{ 
    public static class IdentityExtension 
    { 
     public static string FullName(this IIdentity identity) 
     { 
      var claim = ((ClaimsIdentity)identity).FindFirst("FullName"); 
      return (claim != null) ? claim.Value : string.Empty; 
     } 

     public static string Organization(this IIdentity identity) 
     { 
      var claim = ((ClaimsIdentity)identity).FindFirst("Organization"); 
      return (claim != null) ? claim.Value : string.Empty; 
     } 

     public static string Role(this IIdentity identity) 
     { 
      var claim = ((ClaimsIdentity)identity).FindFirst("Role"); 
      return (claim != null) ? claim.Value : string.Empty; 
     } 

     public static string ProfileImage(this IIdentity identity) 
     { 
      var claim = ((ClaimsIdentity)identity).FindFirst("ProfileImage"); 
      return (claim != null) ? claim.Value : string.Empty; 
     } 

     public static string Email(this IIdentity identity) 
     { 
      var claim = ((ClaimsIdentity)identity).FindFirst("Email"); 
      return (claim != null) ? claim.Value : string.Empty; 
     } 
    } 

} 

は、その後、私は使用することができ、私は自分自身を知りたい原因これは、答えます願っています例

@using Microsoft.AspNetCore.Identity 
@using Core.Extensions 
@{ 
    ViewData["Title"] = "Overview"; 
} 
<h4 class="mt-0 mb-5">Welcome back @User.Identity.FullName()</h4> 
関連する問題