PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Security/CustomPrincipal.cs

#
C# | 35 lines | 27 code | 5 blank | 3 comment | 4 complexity | 19f28c808b30cc70e1ba2771dc0003bb MD5 | raw file
 1namespace BlogEngine.Core
 2{
 3    using System;
 4    using System.Collections.Generic;
 5    using System.Linq;
 6    using System.Text;
 7    using System.Security.Principal;
 8    using System.Web.Security;
 9
10    public class CustomPrincipal : IPrincipal
11    {
12        private IIdentity _identity;
13        public IIdentity Identity
14        {
15            get { return _identity; }
16        }
17
18        public bool IsInRole(string roleName)
19        { 
20            if (Identity == null || !Identity.IsAuthenticated || string.IsNullOrEmpty(Identity.Name))
21                return false;
22
23            // Note: Cannot use "Security.CurrentUser.IsInRole" or anything similar since
24            // Security.CurrentUser.IsInRole will look to this IsInRole() method here --
25            // resulting in an endless loop.  Need to query the role provider directly.
26
27            return Roles.IsUserInRole(Identity.Name, roleName);
28        }
29
30        public CustomPrincipal(IIdentity identity)
31        {
32            _identity = identity;
33        }
34    }
35}