예제 #1
0
    private static void AddAuthenticationCookies(HttpContext context, System.Net.HttpWebRequest req)
    {
        // Copy user identity to new cookie
        if (context.User != null && context.User.Identity != null)
        {
            var cookiesString0 = req.Headers[HttpRequestHeader.Cookie];
            var cookies        = CookieParser.Parse(cookiesString0).ToList();

            // Pass username
            var userCookie = new Cookie("")
            {
                Name = "username", Value = context.User.Identity.Name
            };
            cookies.Add(userCookie);// Always add because this

            // Pass user groups
            var windowsIdentity = context.User.Identity as System.Security.Principal.WindowsIdentity;
            var groupNames      = new string[] { };
            if (windowsIdentity.Groups != null)
            {
                groupNames = windowsIdentity.Groups.Select(g => g.Translate(typeof(System.Security.Principal.NTAccount)).Value).ToArray();
            }
            var groupsCookie = new Cookie("")
            {
                Name = "usergroups", Value = string.Join(",", groupNames)
            };
            cookies.Add(groupsCookie);

            // Authentication ticket
            var ticket       = TicketHandler.IssueTicket();
            var ticketCookie = new Cookie("")
            {
                Name = "winauthticket", Value = ticket
            };
            cookies.Add(ticketCookie);

            var cookiesString = CookieParser.ToString(cookies.ToArray());
            req.Headers[HttpRequestHeader.Cookie] = cookiesString;
        }
    }