5

I want all people from our local Active Directory to be able to login into our blazor server side application. On MSDN they offer me all kind of authentication options. But non of these are for a local Active Directory, just for an Active Directory in Azure.

Windows Authentication is no option because I need the user to be able to logout of the app. Does anyone know a reliable way to achive an authentication with a local Active Directory? Any help is appreciated.

Marvin Klein
  • 1,436
  • 10
  • 33

2 Answers2

2

First add nutget package System.DirectoryServices then in your login component

    @using System.DirectoryServices


<EditForm Model="@user" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <div class="row">
        <div class="col-md-12">
            <label>User Name :</label>
            <InputText @bind-Value="user.UserName"></InputText>
        </div>
        <div class="col-md-12">
            <label>Password</label>
            <InputText @bind-Value="user.Password"></InputText>
        </div>
        <div class="col-md-12">
            <button type="submit">Login</button>
        </div>
    </div>

</EditForm>

@code {

    public UserData user { get; set; } = new UserData();
    private void HandleValidSubmit()
    {
        DirectoryEntry entry = new DirectoryEntry();
        entry = new DirectoryEntry("LDAP://Your Active directory IP");
        entry.Username = user.UserName;
        entry.Password = user.Password;

        DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = "(SAMAccountName=" + user.UserName + ")";

        SearchResult result = search.FindOne();

        if (result == null)
        {
            //return false;
        }
        else
        {
           // return true;
        }


    }

}
  • I'm facing the same problem and querying user/password against AD works just fine. But what's the next step? How do I set the user as "authenticated"? – d00d Nov 15 '21 at 13:30
  • Thank you so much, unfortunately it doesn't work for me. `FindOne()` or `FindAll()` return NULL . – Shadman Sep 12 '22 at 11:24
0
var directorySearch = new DirectorySearcher(new DirectoryEntry("LDAP://" + domain, user.UserName, password));
            directorySearch.Filter = "(SAMAccountName=" + user.UserName+ ")";
            SearchResult result = directorySearch.FindOne();
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
sonu
  • 1
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 24 '23 at 00:40