当前位置: 首页>>代码示例>>C#>>正文


C# GenericIdentity.AddClaim方法代码示例

本文整理汇总了C#中System.Security.Principal.GenericIdentity.AddClaim方法的典型用法代码示例。如果您正苦于以下问题:C# GenericIdentity.AddClaim方法的具体用法?C# GenericIdentity.AddClaim怎么用?C# GenericIdentity.AddClaim使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Principal.GenericIdentity的用法示例。


在下文中一共展示了GenericIdentity.AddClaim方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateIdentity

 private GenericIdentity CreateIdentity(string username, User modelUser)
 {
     var identity = new GenericIdentity(username, BasicScheme);
     identity.AddClaim(new Claim(ClaimTypes.Sid, modelUser.UserId.ToString()));
     identity.AddClaim(new Claim(ClaimTypes.GivenName, modelUser.Firstname));
     identity.AddClaim(new Claim(ClaimTypes.Surname, modelUser.Lastname));
     identity.AddClaim(new Claim(ClaimTypes.Email, modelUser.Email));
     return identity;
 }
开发者ID:EssieShei,项目名称:Mvc4ServicesBook,代码行数:9,代码来源:BasicAuthenticationMessageHandler.cs

示例2: MockPrincipal

        public MockPrincipal(string userId)
        {
            if (default(string) == userId)
                userId = Guid.NewGuid().ToString();
            var identity = new GenericIdentity(userId);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId));
            identity.AddClaim(new Claim(ClaimTypes.Name, userId));
            this.Identity = identity;
        }
开发者ID:blackbarlabs,项目名称:BlackBarLabs.Api,代码行数:10,代码来源:MockPrincipal.cs

示例3: GetPrincipal

        private IPrincipal GetPrincipal(User user)
        {
            var identity = new GenericIdentity(user.Username, Constants.SchemeTypes.Basic);

            identity.AddClaim(new Claim(ClaimTypes.GivenName, user.Firstname));
            identity.AddClaim(new Claim(ClaimTypes.Surname, user.Lastname));

            var username = user.Username.ToLowerInvariant();
            switch(username)
            {
                case "bhogg":
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.Manager));
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.SeniorWorker));
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.JuniorWorker));
                    break;
                case "jbob":
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.SeniorWorker));
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.JuniorWorker));
                    break;
                case "jdoe":
                    identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.JuniorWorker));
                    break;
                default:
                    return null;
            }

            return new ClaimsPrincipal(identity);

        }
开发者ID:sanghyukp,项目名称:AngularJSWebAPI2,代码行数:29,代码来源:BasicSecurityService.cs

示例4: GetPrincipal

        /// <summary>
        /// Builds the Principal
        /// </summary>
        public virtual IPrincipal GetPrincipal(User user)
        {
            var identity = new GenericIdentity(user.UserName, Constants.SchemeTypes.Basic);

            identity.AddClaim(new Claim(ClaimTypes.GivenName, user.FirstName));
            identity.AddClaim(new Claim(ClaimTypes.Surname, user.LastName));
            identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.User));

            var username = user.UserName.ToLowerInvariant();
            if (username == "bhogg")
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, Constants.RoleNames.Admin));
            }

            return new ClaimsPrincipal(identity);
        }
开发者ID:camjm,项目名称:BeyondEarthApp,代码行数:19,代码来源:BasicSecurityService.cs

示例5: SetupIdentity

        private void SetupIdentity(string osmUserId = "42")
        {
            var identity = new GenericIdentity(osmUserId);
            identity.AddClaim(new Claim(ClaimTypes.Name, osmUserId));

            Thread.CurrentPrincipal = new GenericPrincipal(identity, new string[0]);
        }
开发者ID:IsraelHikingMap,项目名称:Site,代码行数:7,代码来源:UrlsControllerTests.cs

示例6: CreateIdentity

        private GenericIdentity CreateIdentity(string username, User modelUser)
        {
            var identity = new GenericIdentity(username, BasicScheme);
            identity.AddClaim(new Claim(ClaimTypes.Sid, modelUser.Id.ToString()));

            return identity;
        }
开发者ID:kanayabhattad,项目名称:EventKarma,代码行数:7,代码来源:BasicAuthenticationMessageHandler.cs

示例7: SetPrincipal

        /// <summary>
        /// This method will create an IIdentity & IPrincipal objects
        /// and sets it to HttpContext.Current.User property, so that the IPrincipal
        /// object is available across HttpContext.
        /// </summary>
        /// <param name="user">User data</param>
        /// <param name="sessionToken">Session Token key</param>
        /// /// <param name="roles">Roles</param>
        private static void SetPrincipal(User user, string sessionToken,string[] roles)
        {
            var identity = new GenericIdentity(user.UserName, SessionSchema);

            identity.AddClaim(new Claim(ClaimTypes.Sid, user.Id.ToString()));
            identity.AddClaim(new Claim(ClaimTypes.PrimaryGroupSid, user.Payor.Id.ToString()));
            identity.AddClaim(new Claim(CustomClaimType.PayorName.ToString(), user.Payor.Name));
            identity.AddClaim(new Claim(ClaimTypes.PrimarySid, sessionToken));

            
            var principal = new GenericPrincipal(identity, roles);

            Thread.CurrentPrincipal = principal;

            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = principal;
            }
        }
开发者ID:Unleadedblues,项目名称:PayMaster,代码行数:27,代码来源:SessionTokenAuthenticationMessageHandler.cs

示例8: GivenClaimsPrincipal_AndIdentityHasNameIdentifierClaim_WhenGetUserKey_ThenGetNameIdentifierClaimValue

        public void GivenClaimsPrincipal_AndIdentityHasNameIdentifierClaim_WhenGetUserKey_ThenGetNameIdentifierClaimValue()
        {
            string expected = "this is the user key";
            GenericIdentity identity = new GenericIdentity("whatever");
            GenericPrincipal principal = new GenericPrincipal(identity, null);
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, expected));

            string actual = EducationSecurityPrincipal.FindUserKey(principal);

            Assert.AreEqual(expected, actual);
        }
开发者ID:modulexcite,项目名称:StudentSuccessDashboard,代码行数:11,代码来源:EducationSecurityPrincipalTest.cs

示例9: MockIdentity

        private void MockIdentity()
        {
            // http://forums.asp.net/t/2028867.aspx?UnitTest+How+to+Mock+User+Identity+GetUserId+
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();

            var identity = new GenericIdentity("[email protected]");
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "1"));
            var principal = new GenericPrincipal(identity, new[] { "user" });
            context.Setup(s => s.User).Returns(principal);

            this.controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), this.controller);
        }
开发者ID:MystFan,项目名称:Heroically-Recipes,代码行数:13,代码来源:VotesControllerTests.cs

示例10: ShouldRedirectWhenDeviceIsCreated

        public async Task ShouldRedirectWhenDeviceIsCreated()
        {
            Device device = new Device { Name = "Raspberry Pi" };
            var mock = new Mock<IDeviceRepository>();
            mock.Setup(m => m.GetDeviceByNameAsync(device.Name))
                .ReturnsAsync(null);

            var context = new Mock<HttpContextBase>();
            var identity = new GenericIdentity("[email protected]");
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "1"));
            var principal = new GenericPrincipal(identity, new[] { "user" });
            context.Setup(s => s.User).Returns(principal);

            IDeviceRepository deviceRepository = mock.Object;
            IMeasurementUnitRepository measurementUnitRepository = Mock.Of<IMeasurementUnitRepository>();
            DeviceController controller = new DeviceController(deviceRepository, measurementUnitRepository);
            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);

            controller.WithCallTo(c => c.Create(device))
                      .ShouldRedirectTo(c => c.Details(1));
        }
开发者ID:johnnyhansson,项目名称:Metrics,代码行数:21,代码来源:DeviceControllerTests.cs

示例11: SetCurrentIdentity

        internal void SetCurrentIdentity(User user, Role role)
        {
            var identity = new GenericIdentity(user.Username);
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", user.Id.ToString()));

            Thread.CurrentPrincipal = new GenericPrincipal(identity, new[] { role.Name });

            UserHelper.Setup(x => x.GetLoggedUser(It.IsAny<HttpRequestMessage>())).Returns(user);
            UserHelper.Setup(x => x.GetLoggedUserRole(It.IsAny<HttpRequestMessage>())).Returns(new Role { Id = role.Id, Name = role.Name });

            UserController = new UserController(UserHelper.Object);
        }
开发者ID:diegocatlad,项目名称:Backup-Toptal,代码行数:12,代码来源:BaseControllerTest.cs

示例12: LoginMock

        private void LoginMock(bool isAuthenticated)
        {
            var identity = new GenericIdentity(this.user.UserName);
            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", this.user.Id));
            var principal = new GenericPrincipal(identity, new[] { "user" });

            var controllerContext = new Mock<ControllerContext>();

            var httpContext = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            controllerContext.SetupGet(x => x.HttpContext).Returns(httpContext.Object);
            controllerContext.SetupGet(x => x.HttpContext.Request).Returns(request.Object);
            controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal);
            controllerContext.SetupGet(x => x.HttpContext.Request.IsAuthenticated)
                .Returns(isAuthenticated);

            this.meController.ControllerContext = controllerContext.Object;
        }
开发者ID:GOTRAOBDT,项目名称:PhotoContest,代码行数:18,代码来源:MeControllerTests.cs

示例13: CreateIdentity

        private GenericIdentity CreateIdentity(MembershipProviderUser user)
        {
            var identity = new GenericIdentity(user.Username, BasicScheme);
            identity.AddClaim(new Claim(ClaimTypes.Sid, user.UserId));
            identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));

            if (GetAdditionalClaims != null)
            {
                try
                {
                    var claims = GetAdditionalClaims(user);
                    identity.AddClaims(claims);
                }
                catch (Exception exception)
                {
                    const string msg = "Error getting additional claims from caller";
                    Debug.WriteLine(msg + ": " + exception);
                    throw new Exception(msg, exception);
                }
            }

            return identity;
        }
开发者ID:jamiekurtz,项目名称:BasicAuthForWebAPI,代码行数:23,代码来源:BasicAuthenticationMessageHandler.cs


注:本文中的System.Security.Principal.GenericIdentity.AddClaim方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。