Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto migration #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Web.Api.Infrastructure/Data/Repositories/EfRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
using Microsoft.EntityFrameworkCore;
using Web.Api.Core.Interfaces.Gateways.Repositories;
using Web.Api.Core.Shared;
using Web.Api.Infrastructure.Identity;

namespace Web.Api.Infrastructure.Data.Repositories
{

public abstract class EfRepository<T> : IRepository<T> where T : BaseEntity
{
protected readonly AppDbContext _appDbContext;
protected readonly AppIdentityDbContext _appIdentityDbContext;

protected EfRepository(AppDbContext appDbContext)
protected EfRepository(AppDbContext appDbContext, AppIdentityDbContext appIdentityDbContext)
{
_appDbContext = appDbContext;
_appIdentityDbContext = appIdentityDbContext;
_appIdentityDbContext.Database.Migrate();
_appDbContext.Database.Migrate();
}

public virtual async Task<T> GetById(int id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class UserRepository : EfRepository<User>, IUserRepository
private readonly IMapper _mapper;


public UserRepository(UserManager<AppUser> userManager, IMapper mapper, AppDbContext appDbContext): base(appDbContext)
public UserRepository(UserManager<AppUser> userManager, IMapper mapper, AppDbContext appDbContext,AppIdentityDbContext appIdentityDbContext): base(appDbContext,appIdentityDbContext)
{
_userManager = userManager;
_mapper = mapper;
Expand Down
14 changes: 14 additions & 0 deletions src/Web.Api/Controllers/ProtectedController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using Web.Api.Infrastructure.Identity;

namespace Web.Api.Controllers
{
Expand All @@ -8,10 +13,19 @@ namespace Web.Api.Controllers
[ApiController]
public class ProtectedController : ControllerBase
{
private readonly UserManager<AppUser> userManager;

public ProtectedController(UserManager<AppUser> userManager)
{
this.userManager = userManager;
}
// GET api/protected/home
[HttpGet]
public IActionResult Home()
{
var sid = User.Claims.Where(c => c.Type == "id")
.Select(c => c.Value).SingleOrDefault();
var ApiUser = userManager.Users.FirstOrDefault(u => u.Id == sid);
return new OkObjectResult(new { result = true });
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Web.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
// api user claim policy
services.AddAuthorization(options =>
{
options.AddPolicy("ApiUser", policy => policy.RequireClaim(Constants.Strings.JwtClaimIdentifiers.Rol, Constants.Strings.JwtClaims.ApiAccess));
options.AddPolicy("ApiUser", policy => policy.RequireClaim(
Constants.Strings.JwtClaimIdentifiers.Rol, Constants.Strings.JwtClaims.ApiAccess));
});

// add identity
Expand Down