Hi all,
I have to create a custom storage provider to connect to a database web service instead of a direct connection to a database.
So far, I can register a user, but if I tried the process the GetUserIdAsync function is been called twice, the first time the user is OK and the ID in present, the second time, the userId is null, so this throws an exception of course.
Here's the code of my user store (I removed some functions for less length in the post)
namespace PLSD.IdentityStore { /// <summary> /// Engin de gestion des utilisateurs /// custom HQ en remplacement de l'identity store de ASP.Net Core par défaut /// </summary> public class UserStore : IUserStore<PLSDUser>, IUserPasswordStore<PLSDUser>, IUserEmailStore<PLSDUser> { /// <summary> /// La clé et l'id de l'application pour la communication sécurisé entre l'app et le service /// </summary> private string _apikey; private string _appkey; private bool _disposed; private readonly IWebServiceCaller _servicecaller; //service pour appeller le service web PLSDIdentity private readonly AppSettings _appsettings; //Paramètres de l'application public UserStore(string AppKey, string APIKey, IWebServiceCaller ServiceCaller, IOptions<AppSettings> AppSettings) { _apikey = APIKey; _appkey = AppKey; _servicecaller = ServiceCaller; _appsettings = AppSettings.Value; } public Task<IdentityResult> CreateAsync(PLSDUser user, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } Task<HttpResponseMessage> task = Task.Run<HttpResponseMessage>(async () => await _servicecaller.CallService(_appsettings.PLSDService, "/api/account/createasync", HttpMethod.Post, user)); HttpResponseMessage result = task.Result; if (result.StatusCode == System.Net.HttpStatusCode.OK) { return Task.FromResult(IdentityResult.Success); } else { return Task.FromResult(IdentityResult.Success); } } public Task<IdentityResult> DeleteAsync(PLSDUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public void Dispose() { //throw new NotImplementedException(); } public Task<PLSDUser> FindByIdAsync(string userId, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task<string> GetPasswordHashAsync(PLSDUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.PasswordHash); } public Task SetPasswordHashAsync(PLSDUser user, string passwordHash, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); if (passwordHash == null) throw new ArgumentNullException(nameof(passwordHash)); user.PasswordHash = passwordHash; return Task.FromResult<object>(null); } public Task<PLSDUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken = default(CancellationToken)) { Task<HttpResponseMessage> task = Task.Run<HttpResponseMessage>(async () => await _servicecaller.CallService(_appsettings.PLSDService, "/api/account/findbynameasync", HttpMethod.Get, normalizedUserName)); HttpResponseMessage result = task.Result; if (result.StatusCode == System.Net.HttpStatusCode.OK) { Task<string> obj = result.Content.ReadAsStringAsync(); string stringResponse = JsonConvert.DeserializeObject<string>(obj.Result); PLSDUser user = JsonConvert.DeserializeObject<PLSDUser>(stringResponse); return Task.FromResult<PLSDUser>(user); } else { return Task.FromResult<PLSDUser>(null); } } public Task<string> GetNormalizedUserNameAsync(PLSDUser user, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } return Task.FromResult(user.NormalizedUserName); } public async Task<string> GetUserIdAsync(PLSDUser user, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } return await Task.FromResult(user.Id.ToString()); } public async Task<string> GetUserNameAsync(PLSDUser user, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } return await Task.FromResult(user.UserName); } public Task SetNormalizedUserNameAsync(PLSDUser user, string normalizedName, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } user.NormalizedUserName = normalizedName; return Task.FromResult(0); } public Task SetUserNameAsync(PLSDUser user, string userName, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } user.UserName = userName; return Task.FromResult(0); } private void ThrowIfDisposed() { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } } } }
Somebody can tell me why the GetUserAsync gets called twice??
The CreateUserAsync function works OK and create the user in the DB.