首先想说明使用C# 来开发Web 版的CMDB是个人的原始需求, 并没有领导或者公司内部有专门的项目支持我做这个事情, 但是考虑到自己的兴趣和职业发展, 自己向自己提了一个需求 ---搭建一个Web CMDB

之前也有了解过市场上有很多开源的CMDB 和付费的CMDB 产品, 但是总之一句话, 系统是别人的, 不能把公司实际的业务需求100%都涵盖了, 或者说不想使用多余(个人认为)的功能

所以有了总的目标之后一系列的小目标或者分支需求, 接二连三的自己给自己提了一大堆, 一边提需求一边自己从网上找一些资料, 消化吸收, 再在我的环境当中反复验证, 反复找网上的视频和其他文章

下面就简单整理了开发Web CMDB 已经储备了一些需要的基础知识

  1. HTML 基础知识
  2. JavaScript, Jquery, Ajax
  3. C#, MVC基础知识
  4. LDAP 基础


前端页面

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap 的 CSS 文件 -->
<link href="../../Content/bootstrap.min.css" rel="stylesheet" />
<link href="../../Content/Login.css" rel="stylesheet" />
<script src="../../Scripts/jquery-3.4.1.min.js" ></script>
<title>Sign In</title>

</head>
<body style="background-color:#F5F5F5" >
<div class="loginBox"> <img class="user" src="../../Images/photo.png" style ="height:100px;width:100px">
<h3>Please sign in</h3>
<div class="inputBox">
<input id="username" type="text" name="Username" required placeholder="Username">
<input id="password" type="password" name="Password" required placeholder="Password">
</div>
<input id="submit" type="submit" name="Login" value="Login">
<div id="errormsg" style="background-color:red; color:white;font-weight:700"></div>
</div>

<script>

$("#submit").click(function () {
var data = {
"username": $("#username").val(),
"password": $("#password").val()
};
$("#errormsg").text("");
$.ajax({
url: "/Ldap/CheckLogin",
type: "POST",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json",
success: function (data) {
if (data.Success == true) {
window.location.href = "/Ldap/SearchUser";
}
else {
var msg = data.ErrorMsg;
$("#errormsg").text(msg);
}
},
error: function () {
console.log("Error Login Failed");
}
});
});
</script>
</body>
</html>

Model逻辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
namespace TestLDAP.Models
{
public class LDAPHelper
{
private DirectoryEntry de;
/// <summary>
/// 创建LDAP连接
/// </summary>
/// <param name="LDAPPath"></param>
/// <param name="authUsername"></param>
/// <param name="authpwd"></param>
/// <returns></returns>
public bool OpenConnection(string LDAPPath, string authUsername, string authpwd)
{
de = new DirectoryEntry(LDAPPath, authUsername, authpwd);
object objde = de.NativeObject;
if (null == de)
{
return false;
}
else if (de.Properties != null && de.Properties.Count > 0)
{
return true;
}
return false;
}
}
}

Controller 层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.DirectoryServices;
using TestLDAP.Models;
//using System.DirectoryServices.AccountManagement;

namespace TestLDAP.Controllers
{
public class LdapController : Controller
{
public ActionResult Login()
{
return View();
}
public ActionResult CheckLogin( string username,string password )
{
string uname = username;
string upwd = password;
string ldappath = "LDAP://DC=contoso,DC=com";
LDAPHelper objldap = new LDAPHelper();
try
{
bool blresult = objldap.OpenConnection(ldappath, uname, upwd);
if (blresult)
{
//return Json(new { Success = true},JsonRequestBehavior.AllowGet);
Session["Currentuser"] = uname;
return Json(new { Success = true});
//return RedirectToAction("SearchUser");
}
else
{
return Json(new { Success = false });
}
}
catch (Exception ex)
{
string msg = ex.Message;
return Json(new { ErrorMsg = msg });
}
}

public ActionResult SearchUser()
{
if (Session["Currentuser"] == null)
{
return Redirect("/Ldap/Login");
}
else {
return View();
}
}
}
}