5
respostas

AspNet Core Web App com Autenticação

Bom dia Guilherme,

Estou com uma duvida, quero implementar o Identity no meu sistema em core.

A minha duvida é, eu devo usar o Owin no statup tb?

5 respostas

Oi, Thiago.

Você não precisa do Owin pra configurar o AspNetIdentity no Asp Net Core!

Abs!

Show, obrigado!!

Guilherme outra duvida, eu configurei o Identity em minha aplicação, usando o app.UseAuthentication(); dentro do Configure, porem as controllers não estão retornando 401.

exemplo:[Authorize] public class HomeController : Controller

As controllers estão retornando o que?

Você poderia compartilhar conosco o startup.cs?

using GestaoFinanceira.Models.Usuario; using GestaoFinanceira.Repositories; using GestaoFinanceira.Services; using GestaoFinanceira.Services.ContaBancariaService; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Quartz; using Quartz.Impl; using System; using System.Collections.Specialized; using System.Threading.Tasks;

namespace GestaoFinanceira { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration;

    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();



        services.AddSession();
        services.AddDistributedMemoryCache();


        string connectionString = Configuration.GetConnectionString("Default");

        string connectionStringIdentity = Configuration.GetConnectionString("IdentityContextConnection");

        services.AddDbContext<ApplicationContext>(options =>
            options.UseSqlServer(connectionString)
        );

        services.AddDbContext<ApplicationDbIdentityContext>(options =>
         options.UseSqlServer(connectionStringIdentity));

        //services.AddIdentity<UsuarioAplicacao, IdentityRole>()
        // .AddEntityFrameworkStores<ApplicationDbIdentityContext>();           


        services.Configure<IdentityOptions>(options =>
        {
            options.Password.RequireDigit = false;
            options.Password.RequiredLength = 6;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;

            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;

            options.User.RequireUniqueEmail = true;
            options.SignIn.RequireConfirmedEmail = true;


        });


        services.Configure<IISOptions>(options =>
        {
            options.ForwardClientCertificate = false;
        });

        services.AddTransient<ILancamentoRepository, LancamentoRepository>();
        services.AddTransient<ICategoriaRepository, CategoriaRepository>();
        services.AddTransient<ILancamentoSerivce, LancamentoSerivce>();
        services.AddTransient<ISaldoRepository, SaldoRepository>();
        services.AddTransient<ISaldoService, SaldoService>();
        services.AddTransient<INFSeRepository, NFSeRepository>();
        services.AddTransient<INFSeService, NFSeService>();
        services.AddTransient<IContaBancariaRepository, ContaBancariaRepository>();
        services.AddTransient<IContaBancariaService, ContaBancariaService>();
        services.AddTransient<IBancoRepository, BancoRepository>();            

        services.AddMvc();

        services.AddIdentityCore<UsuarioAplicacao>()
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbIdentityContext>()
            .AddSignInManager()
            .AddDefaultTokenProviders();

        services.AddAuthentication()
            .AddCookie(cfg => cfg.SlidingExpiration = true);



        services.AddScoped<IJob, LancamentoJob>(p => new LancamentoJob(p.GetService<ILancamentoSerivce>()));

        RunJob(services);
    }}

Guilherme, resolvi o problema.

Outro agora é quando eu logo, da o erro:

InvalidOperationException: No sign-in authentication handler is registered for the scheme 'Identity.Application'. The registered sign-in schemes are: Cookies. Did you forget to call AddAuthentication().AddCookies("Identity.Application",...)?