2017-03-03 9 views
1

EntityFrameworkなしでIdentity Integrator(UserIdおよびRoleId)を使用して.netコアソリューションを作成しようとしています。私は解決策を作成し、EF(https://taherchhabra.github.io/jekyll/update/2016/09/22/aspnet-core-identity-without-entityframework.html)を削除するためのこの基本的なセットアップに従ってきましたが、これはまだユーザーのための愚かなGUID ID列を持っています。私はそれを変更しようとすると、それが必要とIUserStore<User>から継承ので、私は、私はID値はintの代わりに文字列であることを意味するか、もちろん.netコアID(int IDを使用しないEntityFramework)

Task<User> FindByIdAsync(string userId, CancellationToken cancellationToken)

を実装することを私のUserStoreを作成する際に問題が発生しました。

intを使用するようにEFベースのソリューションを変更する方法についていくつかお話しましたが、EFの方法を掘り下げましたが、それはすべてTKeyを使用するEFクラスに基づいています。

文字列をintに変更するために私自身のIUserStoreを作成しようとしましたが、プロジェクトはスピンアップしませんでした(コンパイルはうまくいきましたが、ローカルで実行しようとするとスピンアップを終了します)。

誰でもこれを達成する方法を知っていますか?私はかなり検索して何も見つけることができません。

編集: 私がこれまでに行っていることは(基本的に私が働いて得れば、私は微調整します原稿の単なるコピーです)私自身USERSTOREとRollStoreを作成することです。私にHomeControllerで、私は次のように追加しました:

public class HomeController : Controller 
{ 
    private readonly UserManager<models.User> _userManager; 
    public HomeController(UserManager<models.User> userManager) 
    { 
     _userManager = userManager; 
    } 

    public async Task<IActionResult> Index() 
    { 
     var user = new models.User(); 
     if(User.Identity.IsAuthenticated) 
      user = await _userManager.FindByNameAsync(User.Identity.Name); 

     return View(); 
    } 

} 

これは基本的に私は、デバッガでステップ実行できるテスト「それが差し込まれている」です。私Startup.cs

services.AddSingleton<IUserStore<User>, UserStore>(); 
services.AddSingleton<IRoleStore<Role>, RoleStore>(); 

services.AddIdentity<User, Role>() 
    .AddDefaultTokenProviders(); 

は、これは実際に動作します(私のエンドポイントを通じて、デバッグ、userが正しく装着されています)。しかし、私が近く見ると、私はIUserStoreIRoleStoreMicrosoft.AspNetCore.Identityバージョンであることを認識しています。これは私がまだそれらの契約に依存していることを意味し、IdのためにGuidを使用する必要があります。

私は

services.AddSingleton<SandboxCoreDapper.Identity.Interfaces.IUserStore<User>, UserStore>(); 
services.AddSingleton<SandboxCoreDapper.Identity.Interfaces.IRoleStore<Role>, RoleStore>(); 

を使用して、問題を強制する場合、私は自分のエンドポイントをヒットしようとすると、その後、のUserManagerが解決に失敗します。

型「Microsoft.AspNetCoreのためのサービスを解決することができません。」 Identity.IUserStore 1[SandboxCoreDapper.Models.User]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1 [SandboxCoreDapper.Models.User] '。 "

私はこの興味深い議論見つけ 2編集:

namespace SandboxCoreDapper.Identity.Services 
{ 

    public class UserManager<TUser> : Microsoft.AspNetCore.Identity.UserManager<TUser> where TUser : class 
    { 

     public UserManager(
       sandyId.IUserStore<TUser> store, // my version of IUserStore 
       IOptions<IdentityOptions> optionsAccessor, 
       IPasswordHasher<TUser> passwordHasher, 
       IEnumerable<IUserValidator<TUser>> userValidators, 
       IEnumerable<IPasswordValidator<TUser>> passwordValidators, 
       ILookupNormalizer keyNormalizer, 
       IdentityErrorDescriber errors, 
       IServiceProvider services, 
       ILogger<UserManager<TUser>> logger) 
      :base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger) 
     { 

     } 

    } 
} 

:それ、私は先に行って、私自身が作成したに基づいてhttps://github.com/aspnet/Identity/issues/493

を基本的にはアイデンティティのバージョンを拡張RoleManagerUserManagerRoleManagerについても同様である。

ちょうどあなた自身のUSERSTOREを宣言:

は、その後、私は

+0

これを見てみてください:http://stackoverflow.com/questions/36184856/authentication-and-authorization-with-entity-framework-in-asp-net-5-mvc-6 – ganders

+0

ありがとうございます。私はその質問を読んだが、私は "あなたはuserstoreとrolestoreを実装している場合は組み込みのUserManagerを使うことができます。そうしなければ他の理由がない限り自分で実装する必要はありません。私はこれをしようとしていますが、 'UserManager'はデフォルトのIdentity' UserStore'を指し続けています。私はそれを自分自身のバージョンにポイントする方法を理解できません。私が主題で見つけることができる唯一の記事はIdentity 2のためのものです - 前のコア – nurdyguy

+0

あなたはあなたのUserStoreをstartup.csファイルに登録しましたか? – ganders

答えて

0

私はこのための答えに来る可能性が最も近いが、このgitのですプロジェクト:https://github.com/grandchamp/Identity.Dapper

これはまだ進行中ですが、いくつかの機能強化についてはプロジェクトオーナーと一緒に作業していますが、この質問のうわさは、今すぐプロジェクトがあなたの最善の策です。

1

これは、私のコメントに反応していると、あなたの質問/コメント...

services.AddIdentity<User, Role>() 
    .AddDefaultTokenProviders() 
    .AddUserManager<SandboxCoreDapper.Identity.Services.UserManager<User>>() 
    .AddRoleManager<SandboxCoreDapper.Identity.Services.RoleManager<Role>>() 
      ; 

これまでのところは良いStartup.csに追加しましたそれをStartup.csに登録します。ここで

は、このシナリオに対応するためにUSERSTOREの私の実装です:

public class UserStore : IUserPasswordStore<Login>, IUserEmailStore<Login>, IUserPhoneNumberStore<Login> 
    { 
     private readonly IAccountRepo _accountRepo; 

     public UserStore(IAccountRepo accountRepo) 
     { 
      _accountRepo = accountRepo; 
     } 

     public void Dispose() 
     { 
     } 

     public async Task<string> GetUserIdAsync(Login user, CancellationToken cancellationToken) 
     { 
      return await Task.FromResult(user.Username); 
     } 

     public async Task<string> GetUserNameAsync(Login user, CancellationToken cancellationToken) 
     { 
      return await Task.FromResult(user.Username); 
     } 

     public Task SetUserNameAsync(Login user, string userName, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public Task<string> GetNormalizedUserNameAsync(Login user, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public async Task SetNormalizedUserNameAsync(Login user, string normalizedName, CancellationToken cancellationToken) 
     { 
      await Task.FromResult(user.Username = normalizedName.ToLower()); 
     } 

     public async Task<IdentityResult> CreateAsync(Login user, CancellationToken cancellationToken) 
     { 
      await _accountRepo.CreateLogin(user); 
      return IdentityResult.Success; 
     } 

     public async Task<IdentityResult> UpdateAsync(Login user, CancellationToken cancellationToken) 
     { 
      await _accountRepo.UpdateLogin(user.LoginId, user.Email, true); 
      return IdentityResult.Success; 
     } 

     public Task<IdentityResult> DeleteAsync(Login user, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public async Task<Login> FindByIdAsync(string userId, CancellationToken cancellationToken) 
     { 
      return await _accountRepo.GetUser(userId); 
     } 

     public async Task<Login> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken) 
     { 
      return await _accountRepo.GetUser(normalizedUserName); 
     } 

     public async Task SetPasswordHashAsync(Login user, string passwordHash, CancellationToken cancellationToken) 
     { 
      if (user.LoginId != 0) 
      { 
       await _accountRepo.ChangePassword(user.Email, user.Username, passwordHash); 
      } 

      user.PasswordHash = passwordHash; 
     } 

     public async Task<string> GetPasswordHashAsync(Login user, CancellationToken cancellationToken) 
     { 
      return await Task.FromResult(user.PasswordHash); 
     } 

     public async Task<bool> HasPasswordAsync(Login user, CancellationToken cancellationToken) 
     { 
      return await Task.FromResult(!string.IsNullOrEmpty(user.PasswordHash) && !string.IsNullOrEmpty(user.Salt)); 
     } 

     public Task SetEmailAsync(Login user, string email, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public Task<string> GetEmailAsync(Login user, CancellationToken cancellationToken) 
     { 
      return Task.FromResult(user.Email); 
     } 

     public async Task<bool> GetEmailConfirmedAsync(Login user, CancellationToken cancellationToken) 
     { 
      return await Task.FromResult(user.EmailConfirmed); 
     } 

     public Task SetEmailConfirmedAsync(Login user, bool confirmed, CancellationToken cancellationToken) 
     { 
      return Task.FromResult(user.EmailConfirmed = confirmed); 
     } 

     public Task<Login> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public Task<string> GetNormalizedEmailAsync(Login user, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public Task SetNormalizedEmailAsync(Login user, string normalizedEmail, CancellationToken cancellationToken) 
     { 
      return Task.FromResult(user.Email = normalizedEmail.ToLower()); 
     } 

     public Task SetPhoneNumberAsync(Login user, string phoneNumber, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public Task<string> GetPhoneNumberAsync(Login user, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public Task<bool> GetPhoneNumberConfirmedAsync(Login user, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 

     public Task SetPhoneNumberConfirmedAsync(Login user, bool confirmed, CancellationToken cancellationToken) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

そしてStartup.cs ConfigureServices方法:

services.AddSingleton<IUserStore<Login>, UserStore>(); 
+0

私は現在抱えているポイントで質問を更新します。 – nurdyguy

関連する問題