0

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'.

enter image description here

2
  • 2
    You need to look at the definition of UserManager<TUser> and copy the constraint for TUser to your method definition's constraint for TUser Commented May 11, 2020 at 20:38
  • 1
    Please add error messages as text instead of screenshots. Commented May 11, 2020 at 20:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I get it. Well I am not using aspnet identity core. I am using simple aspnet identity Framework using Microsoft.AspNet.Identity.EntityFramework 2.0. How can i achieve this using without aspnet identity core?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.