Reputation:
I am using additional validation using AspnetIdentity but it is not working gives me this error. Here is my code for interface.
using Health.BLL.CustomModels.UserModels;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Health.BLL.AuthRepository
{
public interface IAuthRepository
{
Task<IdentityResult> RegisterUser(UserModel userModel);
Task<IdentityUser> FindUser(string userName, string password);
Task<IList<string>> GetRolesAsync(string userid);
Task<IdentityResult> ValidateAsync<TUser>(UserManager<TUser> manager, TUser user) where TUser : class;
}
}
For error reference please see the below image.
The type 'TUser' cannot be used as type parameter 'TUser' in the generic type or method 'UserManager'. There is no implicit reference conversion from 'TUser' to 'Microsoft.AspNet.Identity.IUser'.
Upvotes: 0
Views: 224
Reputation: 61
The generic type passed to UserManager needs to implement the interface IUser<TKey>
, where TKey is your user's key type, it can be string, int, etc.
So, assuming that your user key type (TKey) is string, change the contract of ValidateAsync to:
Task<IdentityResult> ValidateAsync<TUser>(UserManager<TUser> manager, TUser user)
where TUser : class, IUser<string>;
Refer to the documentation for more details: https://learn.microsoft.com/en-us/previous-versions/aspnet/dn468199(v=vs.108
Try it, and let me know if it works.
Upvotes: 1
Reputation: 61
Muhammad, maybe there's something wrong with the Nuget Package you're using?...
I've tried to reproduced your error, but everything works fine! I tested your code in a project with Asp.Net Core 2.2, and in another with Asp.Net Core 3.0/3.1.
For the project in Asp.Net Core 2.2, I installed the Nuget Package Microsoft.AspNet.Identity.EntityFrameworkCore Version 2.2.3.
And for the project made in Asp.Net Core 3.0/3.1, I installed the same Nuget Package but version 3.1.3.
And the code that I used to test:
using Microsoft.AspNetCore.Identity;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace WebApplication1
{
public interface IAuthRepository
{
Task<IdentityUser> FindUser(string userName, string password);
Task<IList<string>> GetRolesAsync(string userid);
Task<IdentityResult> ValidateAsync<TUser>(UserManager<TUser> manager, TUser user) where TUser : class;
}
}
Maybe you should edit your question with the Version of the Asp.Net Framework you're using in your project, and the Nuget Package for Identity that you're using.
Upvotes: 0