Buscar contenidos

jueves, 7 de marzo de 2019

Autenticación SSO Azure Active Directory AD


using Microsoft.AspNetCore.Authentication.AzureAD.UI;

  public void ConfigureServices(IServiceCollection services)
        {
        
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient<IPrincipal>(
                provider => provider.GetService<IHttpContextAccessor>().HttpContext.User);

            services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
                .AddAzureAD(options => Configuration.Bind("AzureAd", options));
            
            services.AddMvc(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));


appsettings.Development.json

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/", //Se puede configurar la pag login de GMG
    "Domain": "dominio.onmicrosoft.com",
    "TenantId": "xxx",
    "ClientId": "xxx",
    "CallbackPath": "/signin-oidc"
  },

    "Logging": {
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
        }
    }
}


Layout

    @if (User.Identity.IsAuthenticated)
                    {
                        <div class="name" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">@User.Claims.FirstOrDefault(x => x.Type.Equals("name")).Value</div>
                        <div class="email">@User.Identity.Name</div>
                    }

x

No hay comentarios:

Publicar un comentario