代码之家  ›  专栏  ›  技术社区  ›  djjailbreak

未处理的异常呈现组件:此平台不支持System.Net.NameResolution。ldap blazor登录页出现错误

  •  0
  • djjailbreak  · 技术社区  · 2 年前

    我正在处理一个登录页面,该页面使用Ldap在单击登录按钮时对AD信息进行身份验证,如果成功,它将重定向到/everly页面。然而,当我单击登录按钮时,我会收到以下错误:

    blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
          Unhandled exception rendering component: System.Net.NameResolution is not supported on this platform.
    System.PlatformNotSupportedException: System.Net.NameResolution is not supported on this platform.
       at System.Net.Dns.GetHostAddressesAsync(String hostNameOrAddress)
       at Novell.Directory.Ldap.Connection.Connect(String host, Int32 port, Int32 semaphoreId)
       at Novell.Directory.Ldap.Connection.Connect(String host, Int32 port)
       at Novell.Directory.Ldap.LdapConnection.Connect(String host, Int32 port)
       at RazorCPSSurplus.Client.Classes.AuthenticationService.Authenticate(String username, String password) in D:\cdebrodie\Documents\Repos\RazorSurplus\RazorCPSSurplus\Client\Classes\AuthenticationService.cs:line 30
       at RazorCPSSurplus.Client.Pages.LoginPage.Login() in D:\cdebrodie\Documents\Repos\RazorSurplus\RazorCPSSurplus\Client\Pages\LoginPage.razor:line 34
       at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
       at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
    u @ blazor.webassembly.js:1
    Ke._internal.dotNetCriticalError @ blazor.webassembly.js:1
    _bound_js_Blazor__internal_dotNetCriticalError @ _bound_js_Blazor__internal_dotNetCriticalError:7
    Ss @ dotnet.7.0.10.pc0ureugfn.js:5
    _mono_wasm_invoke_bound_function @ dotnet.7.0.10.pc0ureugfn.js:14
    $func313 @ 009931b2:0x1d5eb
    $func283 @ 009931b2:0x1cae4
    $func221 @ 009931b2:0xe1d4
    $func220 @ 009931b2:0xd044
    $func8115 @ 009931b2:0x1a2377
    $func2040 @ 009931b2:0x85b46
    $func2038 @ 009931b2:0x85abc
    $func1395 @ 009931b2:0x6889a
    $func313 @ 009931b2:0x1d66f
    $func283 @ 009931b2:0x1cae4
    $func221 @ 009931b2:0xe1d4
    $func220 @ 009931b2:0xd044
    $func8115 @ 009931b2:0x1a2377
    $func2040 @ 009931b2:0x85b46
    $func2045 @ 009931b2:0x861ae
    $func2072 @ 009931b2:0x8826d
    $mono_wasm_invoke_method_ref @ 009931b2:0x9d80
    Module._mono_wasm_invoke_method_ref @ dotnet.7.0.10.pc0ureugfn.js:14
    _Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_BeginInvokeDotNet @ _Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_BeginInvokeDotNet:29
    beginInvokeDotNetFromJS @ blazor.webassembly.js:1
    b @ blazor.webassembly.js:1
    invokeMethodAsync @ blazor.webassembly.js:1
    (anonymous) @ blazor.webassembly.js:1
    invokeWhenHeapUnlocked @ blazor.webassembly.js:1
    S @ blazor.webassembly.js:1
    A @ blazor.webassembly.js:1
    dispatchGlobalEventToAllElements @ blazor.webassembly.js:1
    onGlobalEvent @ blazor.webassembly.js:1
    

    这是loginpage.razor代码:

    @page "/login"
    @inject RazorCPSSurplus.Client.Classes.AuthenticationService authService
    @inject NavigationManager NavigationManager
    
    <h1>Login</h1>
    
    @if (loginFailed)
    {
        <p class="text-danger">Login failed. Please try again.</p>
    }
    
    <form>
        <div class="form-group">
            <label for="username">Username:</label>
            <input type="text" class="form-control" id="username" @bind="username">
        </div>
        <div class="form-group">
            <label for="password">Password:</label>
            <input type="password" class="form-control" id="password" @bind="password">
        </div>
        <button type="button" class="btn btn-primary" @onclick="Login">Login</button>
    </form>
    
    @code {
        private string username;
        private string password;
        private bool loginFailed = false;
    
        private async Task Login()
        {
            loginFailed = false;
    
            // Perform LDAP authentication
            if (await authService.Authenticate(username, password))
            {
                // Redirect to the surplus page on successful login
                NavigationManager.NavigateTo("/surplus");
            }
            else
            {
                loginFailed = true;
            }
        }
    }
    

    这里是它使用的AuthenticationService类(出于安全原因,去掉了服务器url和域信息):

    using Novell.Directory.Ldap;
    using System;
    using System.Threading.Tasks;
    
    namespace RazorCPSSurplus.Client.Classes
    {
        public class AuthenticationService
        {
            private readonly LdapConnection _ldapConnection;
            private readonly string _ldapServer;
            private readonly int _ldapPort;
            private readonly string _ldapDomain;
    
            public AuthenticationService()
            {
                _ldapServer = "ldapurl goes here"; // Replace with your LDAP server address
                _ldapPort = 636; // Replace with your LDAP server port
                _ldapDomain = "columbia"; // Replace with your LDAP domain
    
                _ldapConnection = new LdapConnection
                {
                    SecureSocketLayer = false,
                };
            }
    
            public async Task<bool> Authenticate(string username, string password)
            {
                try
                {
                    _ldapConnection.Connect(_ldapServer, _ldapPort);
                    _ldapConnection.Bind($"cn={username},{_ldapDomain}", password);
    
                    return true; // Authentication successful
                }
                catch (LdapException)
                {
                    return false; // Authentication failed
                }
                finally
                {
                    _ldapConnection.Disconnect();
                }
            }
        }
    }
    

    任何帮助都将是惊人的!!!!!

    0 回复  |  直到 2 年前