认证后创建XML文件C#中的用户名

时间:2015-12-09 18:47:37

标签: c# xml cas

我正在进行cas身份验证。我在验证用户然后创建不同用户名角色的xml文件后尝试使用USER名称。稍后将使用此xml文件。 在这里,我试图创建显示问题的XML文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Xml;
using System.Data.SqlClient;
using System.Data;



namespace WebApplication3
{
   public partial class WebForm1 : System.Web.UI.Page
   {
    public WebForm1()
    {
        //InitializeComponent();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();



        Response.ContentType = "text/xml";
        Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        Response.Write("<?xml-stylesheet type=\"text/xsl\" ?>\r\n");

        Response.Write("<Users>\r\n");

        String UserName = "USERname";
        DirectoryEntry de = new DirectoryEntry("ldap://URL", "USERname", "PAssword");

        // object obj = de.NativeObject;
        DirectorySearcher searcher = new DirectorySearcher(de);
        searcher.Filter = "(&(ObjectClass=user)(sAMAccountName=" + UserName + "))";

        searcher.PropertiesToLoad.Add("memberOf");
        StringBuilder groupNames = new StringBuilder(); //stuff them in | delimited

        try
        {

            SearchResult result = searcher.FindOne();

            int propertyCount = result.Properties["memberOf"].Count;
            string dn;
            int equalsIndex, commaIndex;

            for (int propertyCounter = 0; propertyCounter < propertyCount;
                propertyCounter++)
            {
                dn = (string)result.Properties["memberOf"][propertyCounter];
                equalsIndex = dn.IndexOf("=", 1);
                commaIndex = dn.IndexOf(",", 1);
                if (-1 == equalsIndex)
                {
                    Response.Write(null);
                }



                 Response.Write("<User>\r\n");
                 Response.Write("<UserName>" + UserName + "</UserName>\r\n");
                 Response.Write("<Roles>" + groupNames.ToString() + "</Roles>\r\n");

                  Response.Write("</User>\r\n");

            }
              Response.Write("<Users>\r\n");

        }


        catch (Exception ex)
        {
            throw new Exception("Error obtaining group names. " + ex.Message);
        }

        // Response.Write(groupNames.ToString()); 
       }
    }
}

0 个答案:

没有答案