本文整理汇总了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;
}
示例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;
}
示例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);
}
示例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);
}
示例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]);
}
示例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;
}
示例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;
}
}
示例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);
}
示例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);
}
示例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));
}
示例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);
}
示例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;
}
示例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;
}