活动目录自动提取 [英] Active directory auto fetch

查看:57
本文介绍了活动目录自动提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用于获取活动目录的代码。

您可以在下面看到用户应手动输入用户名来检索其详细信息。

dssearch.Filter =(sAMAccountName =+ txtusername.Text +);



但是我想进行自动提取,所以当用户登录时应该捕获他们的sAMAccountName并立即在屏幕上获取它。



请帮帮我...







 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Configuration;
使用 System.DirectoryServices;


public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}
protected void btnSubmit_Click( object sender,EventArgs e)
{
{
string connection = ConfigurationManager.ConnectionStrings [ ADConnection]的ToString();
DirectorySearcher dssearch = new DirectorySearcher(连接);
// dssearch.Filter =(sAMAccountName =+ txtusername.Text +);
dssearch.Filter = (sAMAccountName = + txtusername.Text + ;
SearchResult sresult = dssearch.FindOne();
DirectoryEntry dsresult = sresult.GetDirectoryEntry();
lblfname.Text = dsresult.Properties [ givenName] [ 0 ]的ToString();
lbllname.Text = dsresult.Properties [ sn] [ 0 ]的ToString();
lblemail.Text = dsresult.Properties [ mail] [ 0 ]的ToString();
lbldisplayname.Text = dsresult.Properties [ displayName] [ 0 ]的ToString();
lbltelephone.Text = dsresult.Properties [ telephoneNumber] [ 0 ]的ToString();
lblusername.Text = dsresult.Properties [ userPrincipalName] [ 0 ]的ToString();
lbldepartment.Text = dsresult.Properties [ department] [ 0 ]的ToString();
lblmanager.Text = dsresult.Properties [ title] [ 0 ]的ToString();
// lblmanager.Text = dsresult.Properties [manager] [0] .ToString();




}
}
}

解决方案

您需要从Active Directory(AD)中提取用户名。然后使用它自动登录您的应用程序。



由于这是一个运行AD并且IIS设置为允许Windows身份验证的网页,因此您可以通过几种方式获取用户的域名和ID。你有可以获得它的Page.Identity,或者如果你构建了一个为你处理所有这些的用户类,你可以从HttpContext中获取它,这就是我所拥有的。我创建了一个包含所有这些的类库,这样我就可以将它包含在我的项目中,并始终可以访问用户对象并通过AD自动进行身份验证,并公开我需要的所有用户属性。



这是一个例子,你可以使用类似下面的内容。使用此代码并查看对象及其包含的内容。如果你没有从这些中获取任何东西,那意味着IIS正在阻止它。此外,如果您将此代码放在网页中,您将需要将HTTPContext更改为Page.Identity。但这至少应该指向正确的方向,并且我认为将其推向另一个类是由于更好的接近,所以我在下面包含了这种类型的代码。



  var  id = HttpContext.Current.User.Identity; 
var domain = HttpContext.Current.User.Identity.Name.Split( \C)(0);
var username = HttpContext.Current.User.Identity.Name.Split( \ C)(1);


The following are the code I am using to fetch active directory.
you can see below the users should manually need to type their username to retrieve their details.
dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";

but I want to make this auto fetch, so when user log on it should capture their sAMAccountName and fetch it immediately in the screen.

please help me...



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.DirectoryServices;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        {
            string connection = ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
            DirectorySearcher dssearch = new DirectorySearcher(connection);
            //dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";
            dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";
            SearchResult sresult = dssearch.FindOne();
            DirectoryEntry dsresult = sresult.GetDirectoryEntry();
            lblfname.Text = dsresult.Properties["givenName"][0].ToString();
            lbllname.Text = dsresult.Properties["sn"][0].ToString();
            lblemail.Text = dsresult.Properties["mail"][0].ToString();
            lbldisplayname.Text = dsresult.Properties["displayName"][0].ToString();
            lbltelephone.Text = dsresult.Properties["telephoneNumber"][0].ToString();
            lblusername.Text = dsresult.Properties["userPrincipalName"][0].ToString();
          lbldepartment.Text = dsresult.Properties["department"][0].ToString();
          lblmanager.Text = dsresult.Properties[ "title"][0].ToString();
           //lblmanager.Text = dsresult.Properties["manager"][0].ToString();




        }
    }
}

解决方案

You need to pull the user name from Active Directory(AD). Then use it to auto-login to your applicaiton.

Since this is a webpage with AD running and IIS is set to allow it Windows authentication, then you get the user's domain and id a few ways. You have the Page.Identity where you can get it or you can get it form the HttpContext if you builda user class that handles all of this for you, which is what I have. I created a class library with all of this so that I can just include it my project and always have access to the user object and automatically authenticated through AD and expose all of the user properties that I need.

Here's an example, you can use something like the following. Play with this code and take a look at the objects and what they contain. If you are not gettting anything from these, that means that IIS is blocking it. Also, if you are putting this code in the web page, you will probalby need to change HTTPContext to Page.Identity. But this should at least point you in the right direction and I think pushing it into another class is probalby the better approached, so I have include that type of code below.

var id = HttpContext.Current.User.Identity;
var domain = HttpContext.Current.User.Identity.Name.Split("\"c)(0);
var username = HttpContext.Current.User.Identity.Name.Split("\"c)(1);


这篇关于活动目录自动提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆