<commit_before>using Signum.Engine.Authorization;
using Signum.Entities.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Principal;
using System.DirectoryServices.AccountManagement;
using Signum.Utilities.Reflection;

namespace Signum.React.Authorization;

#pragma warning disable CA1416 // Validate platform compatibility

public class WindowsAuthenticationServer
{
    private static PrincipalContext GetPrincipalContext(string domainName, ActiveDirectoryConfigurationEmbedded config)
    {
        if (config.DirectoryRegistry_Username.HasText())
            return new PrincipalContext(ContextType.Domain, domainName, config.DirectoryRegistry_Username + "@" + config.DomainServer, config.DirectoryRegistry_Password);

        return new PrincipalContext(ContextType.Domain, domainName); //Uses current user
    }

    public static string? LoginWindowsAuthentication(ActionContext ac)
    {
        using (AuthLogic.Disable())
        {
            try
            {
                if (!(ac.HttpContext.User is WindowsPrincipal wp))
                    return $"User is not a WindowsPrincipal ({ac.HttpContext.User.GetType().Name})";

                if (AuthLogic.Authorizer is not ActiveDirectoryAuthorizer ada)
                    return "No AuthLogic.Authorizer set";

                var config = ada.GetConfig();<commit_msg>add DomainServer case in GetPrincipalContext<commit_after>using Signum.Engine.Authorization;
using Signum.Entities.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Principal;
using System.DirectoryServices.AccountManagement;
using Signum.Utilities.Reflection;

namespace Signum.React.Authorization;

#pragma warning disable CA1416 // Validate platform compatibility

public class WindowsAuthenticationServer
{
    private static PrincipalContext GetPrincipalContext(string domainName, ActiveDirectoryConfigurationEmbedded config)
    {
        if (config.DirectoryRegistry_Username.HasText())
            return new PrincipalContext(ContextType.Domain, domainName, config.DirectoryRegistry_Username + "@" + config.DomainServer, config.DirectoryRegistry_Password);

        if (config.DomainServer.HasText())
            return new PrincipalContext(ContextType.Domain, config.DomainServer);
        
        return new PrincipalContext(ContextType.Domain, domainName); //Uses current user
    }

    public static string? LoginWindowsAuthentication(ActionContext ac)
    {
        using (AuthLogic.Disable())
        {
            try
            {
                if (!(ac.HttpContext.User is WindowsPrincipal wp))
                    return $"User is not a WindowsPrincipal ({ac.HttpContext.User.GetType().Name})";

                if (AuthLogic.Authorizer is not ActiveDirectoryAuthorizer ada)
                    return "No AuthLogic.Authorizer set";

                var config = ada.GetConfig();
{'fraction_non_alphanumeric': 0.07721739130434782, 'fraction_numerical': 0.002782608695652174, 'mean_word_length': 3.3575757575757574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7488161', 'n_tokens_mistral': 698, 'n_tokens_neox': 676, 'n_words': 169}