C# 获取域用户信息并统计域用户访问过的页面

最近,公司让做个网站,因为是内网的,所以为了方便,采用域用户,而不用重新注册。

  1. 下面是根据WindowsPrincipal获取当前域用户的用户名和域

using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Principal;
using System.Threading;

public class MyPrincipal
    {
        WindowsPrincipal wp = (WindowsPrincipal)Thread.CurrentPrincipal;

        /// <summary>
        /// 判断是否是管理员
        /// </summary>
        public bool IsAdmin()
        {
            string adminUsers=Global.WebsiteConfig.SiteConfigValue("ga", "adminUsers");
            return adminUsers.Contains(UserName + ";");
        }

        /// <summary>
        /// 域用户名
        /// </summary>
        public string UserName { 
            get {
                string[] wpArray = wp.Identity.Name.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                return wpArray.Length>0?wpArray[1]:"";
            }
        }

        /// <summary>
        /// 域
        /// </summary>
        public string UserDomain
        {
            get
            {
                string[] wpArray = wp.Identity.Name.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                return wpArray.Length > 0 ? wpArray[0] : "";
            }
        }
    }

    注 :  上面的方法有点麻烦,徐少侠  提供了一种更为简单的方法来获取域用户,如下:

             首先在Web.config里面配置, 在<system.web>内添加 <identity impersonate="true" />
      然后在代码中使用
      System.Environment.UserDomainName
      System.Environment.UserName

              就ok了。再次感谢 徐少侠

2. 因为要统计域用户的访问记录,并且网站页面很多,不可能每个页面都加上统计代码,这里有两个方法解决(限于个人水平,只想到两个方法,应该还有其它方法):

    a.页面基类的方式,即新建一个页面基类(BasePage),继承page类,并重写OnInit方法,将统计代码写入,然后其它页面继承BasePage就可以了。

    b.HttpModule方式,新建一个asp.net 模块,将统计代码写入AcquireRequestState事件中,具体代码如下:

   
public class Counter : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {
            context.EndRequest += new EventHandler(context_AcquireRequestState);
        }

        void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;

            MyPrincipal mp = new MyPrincipal();

            if (mp.UserName!="")
            {

                try
                {
                    Log model = new Log();
                    model.UserName = mp.UserName;
                    model.UserDomain = mp.UserDomain;
                    model.Page = context.Request.Url.ToString();
                    model.SessionID = context.Session.SessionID;
                    model.CreateOn = DateTime.Now.ToString("yyyy-MM-dd");

                    LogService service = new LogService();
                    if (!service.Exists(model))
                    {
                        service.Insert(model);
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        #endregion

    }

注:因为要用到session,所以统计代码写在context_AcquireRequestState事件中,因为在此之前HttpRequest还未交给HttpHandler,session还不存在。并且如果用太靠前的事件,如:BeginRequest等,

WindowsPrincipal wp = (WindowsPrincipal)Thread.CurrentPrincipal; 此处会报错,可能是因为此处CurrentPrincipal还不存在吧。
水平有限,欢迎大家拍砖指正。
posted @ 2011-05-11 15:01  忆起  阅读(2071)  评论(12编辑  收藏  举报