Asked by:
ACTIVE DIRECTORY (LDAP) UNLOCK USER WEB API

Question
-
User2049157405 posted
Hi, Guys. I am beginner and have no experience in .net. I want to create a WEB application for managing LDAP users (New-ADUser, Unlock-ADAccount and others). I have Visual studio 2019 at my disposal. I want to do this with, for example, webforms. Can you help me? :(
WebForm1.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Text; using System.DirectoryServices; namespace ITDropletsPowershell { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public void Unlock(string userDn) { try { DirectoryEntry uEntry = new DirectoryEntry(userDn); uEntry.Properties["LockOutTime"].Value = 0; //unlock account uEntry.CommitChanges(); //may not be needed but adding it anyways uEntry.Close(); } catch (System.DirectoryServices.DirectoryServicesCOMException E) { //DoSomethingWith --> E.Message.ToString(); } } protected void UNLOCKUSERClick(object sender, EventArgs e) { Result.Text = string.Empty; InitialSessionState iss = InitialSessionState.CreateDefault2(); var shell = PowerShell.Create(iss); shell.Commands.AddCommand("Unlock-ADAccount"); shell.Commands.AddParameter("Identity", Input.Text); try { var results = shell.Invoke(); if (results.Count > 0) { var builder = new StringBuilder(); foreach (var psObject in results) { builder.Append(psObject.BaseObject.ToString() + "\r\n"); } Result.Text = Server.HtmlEncode(builder.ToString()); } } catch (ActionPreferenceStopException Error) { Result.Text = Error.Message; } catch (RuntimeException Error) { Result.Text = Error.Message; }; } } }
WebDorm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ITDropletsPowershell.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>USER MANAGER</title> </head> <body> <form id="form2" runat="server"> <div> <div><h1 align="center">UNLOCK USER ACCOUNT</h1></div> <p>PLEASE GIVE USER SKP (ex. FORMAT 123987): <asp:TextBox ID="Input" runat="server" Width="30%" Height="20px" ></asp:TextBox> </p> <asp:Button ID="UNLOCKUSER" runat="server" Text="UNLOCK USER" Width="150" onclick="UNLOCKUSERClick" /> <p>Result <asp:TextBox ID="Result" TextMode="MultiLine" Width="100%" Height="450px" runat="server"></asp:TextBox> </p> </div> </form> </body> </html>
Image web: https://imgur.com/wkqTSrE
Wednesday, February 19, 2020 9:51 PM
All replies
-
User753101303 posted
Hi,
And ????
For this it's likely simpler to use https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.authenticableprincipal.unlockaccount?view=netframework-4.8#System_DirectoryServices_AccountManagement_AuthenticablePrincipal_UnlockAccount that comes with a code sample.
When posting code always be explicit and tell what happens when this code runs. Else we have to take minutes to read the code and GUESS what happens and if spending few minutes in trying your code, we can still run into some other issue especially as here you are using PowerShell under the hood.
My guess is that you have a permission issue ????
Edit: using an empty catch block is likely the last thing you want. It would hide a possible error without anynbody knowing about it...
Wednesday, February 19, 2020 10:34 PM -
User2049157405 posted
PatriceSc thank you for your response. My goal is to issue WEB Api service on a dedicated server with ISS. After entering the user, the user can unblock him in the domain. After pressing UNLOCK it includes a message in the "result" field that the user has been blocked. I apologize for the mistakes so I am asking for your understanding.
Wednesday, February 19, 2020 10:41 PM