using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Security.Principal;
using System.DirectoryServices;
using System.Net.Mail;
using System.Text;
/// <summary>
/// Summary description for Tools
/// </summary>
public class Tools
{
public enum RightType
{
Manager = 1,
Logistics = 2,
IDM=3,
Admin=4
}
// Fields
private string Adpathg = ConfigurationManager.AppSettings["getgroup"].ToString();
private string Adpathu = ConfigurationManager.AppSettings["getuser"].ToString();
// Methods
public string ExtractUserName(string path)
{
string[] strArray = path.Split(new char[] { '//' });
return strArray[strArray.Length - 1];
}
static public string GetUserName(IPrincipal user)
{
return GetUserName(user.Identity.Name);
}
static public string GetUserFullName(IPrincipal user)
{
return GetUserFullName(user.Identity.Name);
}
static public string GetUserName(string strLogin)
{
string fullUserName = strLogin.ToUpper();
char[] split = { '//' };
string[] arrUsers = fullUserName.Split(split);
return arrUsers[arrUsers.Length - 1];
}
/// <summary>
///
/// </summary>
/// <param name="ntAccount">like Asia/Jiyzhou</param>
/// <returns></returns>
static public string GetUserFullName(string ntAccount)
{
string str = "";
// Parse the string to check if domain name is present.
int idx = ntAccount.IndexOf('//');
if (idx == -1)
{
idx = ntAccount.IndexOf('@');
}
string strDomain;
string strName;
if (idx != -1)
{
strDomain = ntAccount.Substring(0, idx);
strName = ntAccount.Substring(idx + 1);
}
else
{
strDomain = Environment.MachineName;
strName = ntAccount;
}
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
object obVal = coll["FullName"].Value;
str = obVal.ToString();
}
catch (Exception ex)
{
str = "";
//Trace.Write(ex.Message);
}
return str;
}
public bool GetAccountname(string loginName)
{
DirectoryEntry searchRoot = new DirectoryEntry(this.Adpathu, ConfigurationManager.AppSettings["logonname"].ToString(), ConfigurationManager.AppSettings["logonpwd"].ToString());
SearchResult result = new DirectorySearcher(searchRoot).FindOne();