作者 主題: .net component to add AD users for calling from vbscrip  (閱讀 5274 次)

0 會員 與 1 訪客 正在閱讀本文。

小徒兒

  • 鑽研的研究生
  • *****
  • 文章數: 622
    • 檢視個人資料
1. new one .net C# .dll project name:BankComponentServer

2. change the class1.cs to Account.cs

add reference System.EnterPriseService

代碼: [選擇]
using System.EnterpriseServices;
using System.DirectoryServices;
using System;




[assembly: ApplicationName("BankComponent")]




namespace BankComponentServer
{
[Transaction(TransactionOption.Required)]
public class Account : ServicedComponent
{
[AutoComplete]
public string Post(int accountNum, double amount,string DepName,string cnFullName, string FirstName, string LastName, string displayFullName,string userAccount,string Password,bool _lock )
{
try
{

System.DirectoryServices.DirectoryEntry de;

de = new DirectoryEntry("LDAP://OU="+DepName+",DC=test,DC=com,DC=tw","Administrator","gotohell");
DirectoryEntries myEntries = de.Children;



DirectoryEntry myDirectoryEntry = myEntries.Add("CN=" +  displayFullName , "user");
myDirectoryEntry.Properties["displayName"].Add(DepName+displayFullName);

myDirectoryEntry.Properties["sn"].Add(LastName);
myDirectoryEntry.Properties["givenName"].Add(FirstName);
myDirectoryEntry.Properties["description"].Add(DepName);

myDirectoryEntry.Properties["sAMAccountName"].Add(userAccount);
myDirectoryEntry.Properties["userPrincipalName"].Add(userAccount + "@nettrade.com.tw");
myDirectoryEntry.Properties["mail"].Add(userAccount + "@nettrade.com.tw");
myDirectoryEntry.Properties["scriptpath"].Add("mount.bat");





//ActiveDs.ADS_USER_FLAG auLock;

//ActiveDs.ADS_USER_FLAG auExpire;

myDirectoryEntry.CommitChanges();

myDirectoryEntry.Invoke("SetPassword", new object[]{Password});


// if(_lock)
// {
// auLock=ActiveDs.ADS_USER_FLAG.ADS_UF_ACCOUNTDISABLE;
// }
// else
// {
// auLock=ActiveDs.ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT;
// }

myDirectoryEntry.Properties["userAccountControl"].Value = 0x10000 | 0X0200;
myDirectoryEntry.CommitChanges();

System.DirectoryServices.DirectoryEntry AD;
AD = new DirectoryEntry("LDAP://DC=test,DC=com,DC=tw","Administrator","gotoheaven");
DirectoryEntry grp = AD.Children.Find("CN="+ DepName  +",CN=Users","group");
//grp.Rename("CN=人力資源部");

grp.Invoke("add",myDirectoryEntry.Path.ToString());


return "true";


}
catch(Exception err)

{
   string errstring= err.ToString();
return errstring ;
}



return "true";

// Updates the database, no need to call SetComplete.





// Calls SetComplete automatically if no exception is thrown.
}
}
}



3. in vs .net command tool ,generate the key

sn -k Demos.snk

4. copy the Demos.snk to the

C:\Documents and Settings\youracount\My Documents\Visual Studio Projects\BankComponentServer\obj\Debug


5. make the dll

6. in the component management. Add this component in the componenet management.

在元件服務中加入此元件
     6.1 - in the componenet service management (元件管理 )
     6.2 - my computer/COM +  application/right click/add new applicaiton (應用程式)
   6.3 - add new appliation/ new one empty application (新增應用程式/建立空的應用程式)
     6.4 - using component name that is the same in the code
              [assembly: ApplicationName("BankComponent")]

               (使用 [assembly: ApplicationName("BankComponent")] 中的名字: BankComponent )

   6.5 - click the checkbox for server application
          (checked: 伺服應用程式 )
   6.6 - using the user who has the webserver system writing authority and can run shell
                 ( 使用下列使用者,可以有寫入webserver 檔案系統及執行shell的 )

                 [keyin the domain\username]
          [passwordofuser]
          [passwordofuser]

     7. on the BankComponent/right click/ new  component
        (伺服應用程式 /元件/右鍵新增元件 )
             7.1 choose install new component
        7.2 choose the just compiled dll file. C
        C:\Documents and Settings\yourusername\My Documents\Visual Studio Projects\BankComponentServer\obj\Debug\BankComponentServer.dll

            (選擇安裝新元件)

選C:\Documents and Settings\yourusername\My Documents\Visual Studio Projects\BankComponentServer\obj\Debug\BankComponentServer.dll

8.choose componet / content/ safety setting /
   click to uncheck the checkbox of restrict component access 選擇元件/內容/ 安全設定 強制元件存取 的勾勾拿掉

9.修改machine code, 如果沒有自行新增機碼

regedit

代碼: [選擇]
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\
    Parameters\ADCLaunch\yourprogid.yourclassname (project and class name of your Business Object)

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\
    Parameters\ADCLaunch\BankComponentServer.Account


10. 可以了,醬就可以call call 看

代碼: [選擇]
<html>
<head>

<Script Language=VBScript>

Dim rds,rs,df,df2,ServerStr
   dim strSQL,StrRs
   dim ME001,ME002

set rds = CreateObject("RDS.Dataspace")
Set df2 = rds.CreateObject("BankComponentServer.Account","<%=Session("http")%><%=Request.ServerVariables("SERVER_NAME")%>")

xx= df2.Post(1,1.0,rs("efsty004")&"",rs("efsty001")&"",right(rs("efsty001")&"",2),Left(rs("efsty001")&"", 1),rs("efsty001")&"",document.all("efsty006").value,document.all("efsty009").value,false)

if xx=true then

msgbox "AD帳號建立成功"
else
msgbox "AD帳號建立失敗,可能有重複名稱已建立,請洽資訊部"

end if


</Script>
</head>
<body>
</body>
</html>



***  .Properties("manager").Value = ("CN=OOO,OU=管理部,OU=aaaaaaaaa,dc=test,dc=com,dc=tw")






代碼: [選擇]
'測試以上所寫的clss
'加入引用 System.EnterpriseServices; BankComponentServer.dll


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using BankComponentServer;
using System.EnterpriseServices;
using System.DirectoryServices;
using System;


namespace WindowsApplication4321
{
/// <summary>
/// Form1 的摘要描述。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
   public BankComponentServer.Account df;
private System.Windows.Forms.Button button2;

/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows Form 設計工具支援的必要項
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
//
}

/// <summary>
/// 清除任何使用中的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
BankComponentServer.Account df = new BankComponentServer.Account();

MessageBox.Show(df.Post(1,1.0,"資訊部","資訊部望測試","測試","旺","望測試","test12345","Password",false));


}

private void button2_Click(object sender, System.EventArgs e)
{

}
}
}




***get the entryproperty
string[] keys = new string[entry.properties.count];
entry.properties.propertynames.copyto(keys,0);

**change entry value
directoryentry.invokeset
directoryentry.commitchanges

**search entry
directorysearcher
findall
searchresultcollection

.filter ="(objecttClass=user)";


**AD authentification
DirectorySearcher
directoryentry
filter
objectclass=user
findone
getdirectoryentry(().properties["objectSid"].value
« 上次編輯: 2008-09-28 21:26 由 小徒兒 »