Ezel Eren Avcı
Ezel Eren Avcı

Reputation: 1

How to get list asp.net web API identity users (and delete,put).. for ASP.NET WEB API?

IdentityModels.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {

    }

ServiceController.cs

public class ServiceController : ApiController
{
    

    [Route("api/user/register")]
    [HttpPost]
    public IdentityResult Register(AccountModel model)
    {
        var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
        var manager = new UserManager<ApplicationUser>(userStore);
        var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email };
        user.PhoneNumber = model.PhoneNumber;
        user.FullName = model.FullName;
        user.TcNo = model.TcNo;
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 3
        };
        IdentityResult result = manager.Create(user, model.Password);
        return result;
        
    }

Web.config

 <connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=DESKTOP-M6EQ5IM\EREN;Initial Catalog=AkunsemDB;Integrated Security=True" providerName="System.Data.SqlClient" />

QUESTİON

How to get list asp.net web API identity users (and delete,put).. for ASP.NET WEB API?

    [Route("api/user/userlist")]
    [HttpGet]????????????????
    ????????????????

Upvotes: 0

Views: 104

Answers (2)

Akalanka Dissanayake
Akalanka Dissanayake

Reputation: 596

Yes, UserManager is a good option but if you want to do more complex queries, The best option is dbcontext. IdentityDbContext will add these tables to your database.

enter image description here

So you can write any LINQ queries based on your requirements. example code to get all role claims

var claims = (from ur in _context.UserRoles
          where ur.UserId == user.Id
          join r in _context.Roles on ur.RoleId equals r.Id
          join rc in _context.RoleClaims on r.Id equals rc.RoleId
          select rc)

Upvotes: 0

Sotiris Panopoulos
Sotiris Panopoulos

Reputation: 1613

In the UserManager helper class, there is the Users property so you can query them.

Also, I would highly recommend you use Dependency Injection in your API. You can take a look at a sample using ASP.NET Identity here

Upvotes: 1

Related Questions