.NET



Create Active Directory User In .NET

C#

    public string CreateADUser(string szUsername, string szDisplayName, string szFirstName, string szLastName, string szMiddleInitial, string szPhone, string szEmail, string szOrgUnit)
    {
        string szPassword = "fooBar_123";  //temp only.
        string szCNName = "CN=" + szUsername;
 
        try
        {
            DirectoryEntry objContainer = new DirectoryEntry("LDAP://OU=mySubOU,OU=myMainOU,DC=KEVINCORNWELL,DC=com");
            DirectoryEntry objUser = objContainer.Children.Add(szCNName.ToLower(), "user");
            objUser.Properties["sAMAccountName"].Value = szUsername;
            objUser.Properties["userPrincipalName"].Value = szUsername + "@KEVINCORNWELL.COM";
            objUser.Properties["displayName"].Value = szDisplayName;
            objUser.Properties["GivenName"].Value = szFirstName;
            objUser.Properties["sn"].Value = szLastName;
            objUser.Properties["Initials"].Value = szMiddleInitial;
            objUser.Properties["mail"].Value = szEmail;
            objUser.Properties["TelephoneNumber"].Value = szPhone;
            objUser.Properties["Description"].Value = szOrgUnit;
            objUser.CommitChanges();
            objUser.Invoke("SetPassword", szPassword);
            objUser.CommitChanges();
            int flags = (int)objUser.Properties["userAccountControl"].Value;
            objUser.Properties["userAccountControl"].Value = flags & ~0x2;  //Enable User Account
            objUser.Properties["pwdLastSet"].Value = 0;
            objUser.CommitChanges();
            return "";
        }
        catch (Exception ex)
        {
            return ex.InnerException + ex.Message + ex.Source + ex.StackTrace + ex.Data;
        }
    }

Post Comment Now


DD-WRT MAC Address Toolkit

This .NET 2.0 website adds additional functionality to associate a name and notes to the MAC addresses in a DD-WRT flashed router. I did this because the built in only has MAC fields which makes it cumbersome to associate names/computer to MACs when you have dozens of clients. The C# source code is included in the zip file. I make use of the open source SharpSSH library to gain access to the router over SSH.

You will have to edit several files for your particular environment.

Screenie

DD-WRT MAC Address Toolkit Website


Post Comment Now


How to dynamically add a table row with input fields in .NET.

C# using Visual Studio 2005…

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page 
{
    // How many rows when the page initially loads.
    static int tDefaultRow_Count = 10;
    // Max number of rows.
    static int tMaxRows = 50;
 
    // Do not modify the following.
    private int tUserAddedRowCount = 0;
    static TextBox[] TextBox_Item = new TextBox[tMaxRows];
    static TextBox[] TextBox_Desc = new TextBox[tMaxRows];
    static TextBox[] TextBox_Quantity = new TextBox[tMaxRows];
    static TextBox[] TextBox_Price = new TextBox[tMaxRows];
    static TextBox[] TextBox_Total = new TextBox[tMaxRows];
 
    protected void Page_Load(object sender, EventArgs e)
    {
        tUserAddedRowCount = (ViewState["tUserAddedRowCount"] != null) ? int.Parse(ViewState["tUserAddedRowCount"].ToString()) : 0;
        //Response.Write("User Rows: " + tUserAddedRowCount + "\r\n");
 
        // Recreate the extra rows created by the user on postback
        // events (required to maintain state).
        Add_Table_Rows(tUserAddedRowCount + tDefaultRow_Count);
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
            // User clicked "add a row" button.
 
            Add_Table_Rows(1);
            ViewState["tUserAddedRowCount"] = ++tUserAddedRowCount;
    }
 
    protected void Button2_Click(object sender, EventArgs e)
    {
        // User clicked "Submit" button.
 
        for (int x = 0; x < tDefaultRow_Count + tUserAddedRowCount; x++)
        {
 
            if (TextBox_Item[x].Text.ToString().Length > 0) 
            { 
                Response.Write(TextBox_Item[x].Text.ToString());
                Response.Write(TextBox_Desc[x].Text.ToString());
                Response.Write(TextBox_Quantity[x].Text.ToString());
                Response.Write(TextBox_Price[x].Text.ToString());
                Response.Write(TextBox_Total[x].Text.ToString());
            }
        }
    }
 
    private void Add_Table_Rows(int NumberOfRows)
    {
        for (int x = 0; x < NumberOfRows; x++)
        {
            TableRow myRow = new TableRow();
            Table1.Rows.Add(myRow);
 
            TableCell myCell0 = new TableCell();
            myRow.Cells.Add(myCell0);
            TextBox myTextbox0 = new TextBox();
            TextBox_Item[x] = myTextbox0;
            myCell0.Controls.Add(TextBox_Item[x]);
 
            TableCell myCell1 = new TableCell();
            myRow.Cells.Add(myCell1);
            TextBox myTextbox1 = new TextBox();
            TextBox_Desc[x] = myTextbox1;
            myCell1.Controls.Add(TextBox_Desc[x]);
 
            TableCell myCell2 = new TableCell();
            myRow.Cells.Add(myCell2);
            TextBox myTextbox2 = new TextBox();
            TextBox_Quantity[x] = myTextbox2;
            myCell2.Controls.Add(TextBox_Quantity[x]);
 
            TableCell myCell3 = new TableCell();
            myRow.Cells.Add(myCell3);
            TextBox myTextbox3 = new TextBox();
            TextBox_Price[x] = myTextbox3;
            myCell3.Controls.Add(TextBox_Price[x]);
 
            TableCell myCell4 = new TableCell();
            myRow.Cells.Add(myCell4);
            TextBox myTextbox4 = new TextBox();
            TextBox_Total[x] = myTextbox4;
            myCell4.Controls.Add(TextBox_Total[x]);
 
        }
    }
}

Post Comment Now


Enable .NET Web Extensions In ISS via Command Line

From command line

C:\WINDOWS\Microsoft.NET\Framework\[framework version]\aspnet_regiis.exe -r

After that has run, goto IIS –> Web Service Extensions –> ASP.NET [framework version] –> Enable.


Post Comment Now


.NET ConnectionString Encyption

The following command will encrypt your connection sting in your web.config file. The connection string must all ready be in place.

For C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG

-pkm switch encrypts the machine.config rather and web.config (default).

aspnet_regiis.exe -pef "connectionStrings" "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config" -prov "DataProtectionConfigurationProvider"

Post Comment Now


Blog | Contact | Gallery | Links | Sandbox | Social Networking | Weather | Web Design


Copyright © 1997-2009 KCSH. All rights reserved.