本文整理汇总了C#中System.Security.Principal.GenericPrincipal.IsInRole方法的典型用法代码示例。如果您正苦于以下问题:C# GenericPrincipal.IsInRole方法的具体用法?C# GenericPrincipal.IsInRole怎么用?C# GenericPrincipal.IsInRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Principal.GenericPrincipal
的用法示例。
在下文中一共展示了GenericPrincipal.IsInRole方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsInRole_CaseInsensitive
public void IsInRole_CaseInsensitive ()
{
GenericIdentity gi = new GenericIdentity ("user");
GenericPrincipal gp = new GenericPrincipal (gi, new string[2] { "mono", "hackers" });
Assert.IsTrue (gp.IsInRole ("MoNo"), "MoNo");
Assert.IsTrue (gp.IsInRole ("hAcKeRs"), "hAcKeRs");
}
示例2: IsInRole
public void IsInRole ()
{
GenericIdentity gi = new GenericIdentity ("user");
string[] roles = new string [5];
roles [0] = "role 1";
GenericPrincipal gp = new GenericPrincipal (gi, roles);
roles [1] = "role 2";
Assert.IsTrue (gp.IsInRole ("role 1"), "IsInRole (role added before constructor)");
Assert.IsFalse (gp.IsInRole ("role 2"), "IsInRole (role added after constructor)");
}
示例3: Main
static void Main(string[] args)
{
/*
IEnumerator enumerator = SecurityManager.PolicyHierarchy();
IEnumerable enumerable = (IEnumerable)enumerator;
Console.WriteLine("Didn't crash and burn like I expected.");
*/
// Create a generic identity.
GenericIdentity myIdentity = new GenericIdentity("MyIdentity");
// Create a generic principal.
string[] myRoles = { "Manager", "Teller" };
GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myRoles);
// Attach
Thread.CurrentPrincipal = myPrincipal;
// Print values to the console.
string name = Thread.CurrentPrincipal.Identity.Name;
bool auth = myPrincipal.Identity.IsAuthenticated;
bool isInRole = myPrincipal.IsInRole("Manager");
Console.WriteLine("The name is: " + name);
Console.WriteLine("IsAuthenticated: " + auth);
Console.WriteLine("Is this a Manager?: " + isInRole);
}
示例4: NullRoles
public void NullRoles ()
{
GenericIdentity gi = new GenericIdentity ("user");
GenericPrincipal gp = new GenericPrincipal (gi, null);
Assert.AreEqual ("user", gp.Identity.Name, "Identity");
Assert.IsFalse (gp.IsInRole ("role 1"), "NoRole.IsInRole(x)");
}
示例5: NullRoles
public void NullRoles ()
{
GenericIdentity gi = new GenericIdentity ("user");
GenericPrincipal gp = new GenericPrincipal (gi, null);
AssertEquals ("Identity", "user", gp.Identity.Name);
Assert ("NoRole.IsInRole(x)", !gp.IsInRole ("role 1"));
}
示例6: SerializationRoundtrip
public void SerializationRoundtrip ()
{
GenericIdentity gi = new GenericIdentity ("mono", "dna");
GenericPrincipal gp = new GenericPrincipal (gi, new string[2] { "monkey", "hackers" });
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream ();
bf.Serialize (ms, gp);
//Console.WriteLine (BitConverter.ToString (ms.ToArray ()));
ms.Position = 0;
GenericPrincipal clone = (GenericPrincipal) bf.Deserialize (ms);
Assert.AreEqual (gp.Identity.Name, clone.Identity.Name, "Name");
Assert.AreEqual (gp.Identity.AuthenticationType, clone.Identity.AuthenticationType, "AuthenticationType");
Assert.AreEqual (gp.Identity.IsAuthenticated, clone.Identity.IsAuthenticated, "IsAuthenticated");
Assert.IsTrue (gp.IsInRole ("monkey"), "IsInRole-monkey");
Assert.IsTrue (gp.IsInRole ("hackers"), "IsInRole-hackers");
Assert.IsFalse (gp.IsInRole ("donkey"), "IsInRole-donkey");
}
示例7: AuthenticateAsync
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
await Task.Run(() =>
{
IPrincipal incomingPrincipal = context.ActionContext.RequestContext.Principal;
Debug.WriteLine(String.Format("Incoming principal in custom auth filter AuthenticateAsync method is authenticated: {0}", incomingPrincipal.Identity.IsAuthenticated));
IPrincipal genericPrincipal = new GenericPrincipal(new GenericIdentity("Andras", "CustomIdentification"), new string[] { "Admin", "PowerUser" });
Debug.WriteLine(String.Format(genericPrincipal.IsInRole("Admin").ToString()));
context.Principal = genericPrincipal;
});
}
示例8: call_protectedByDemandWithNoAuthentication_with_no_roles
public string call_protectedByDemandWithNoAuthentication_with_no_roles()
{
try
{
var principal = new GenericPrincipal(new GenericIdentity(""), new string[] {""});
Assert.That(principal.Identity.IsAuthenticated.isFalse(), "IsAuthenticated should be false");
Assert.That(principal.IsInRole("Admin").isFalse(), "IsInRole(\"Admin\") should be false");
System.Threading.Thread.CurrentPrincipal = principal;
protectedByDemandWithNoAuthentication();
return "Note: the Demand for Role=\"Admin\" was not enforced";
}
catch(Exception ex)
{
Assert.That(ex is System.Security.SecurityException, "Was expecting a SecurityException object, and had: {0}".format(ex.Message));
return "got expected security exception";
}
}
示例9: call_protectedByDemandWithNoAuthentication_with_admin_role
public string call_protectedByDemandWithNoAuthentication_with_admin_role()
{
try
{
var principal = new GenericPrincipal(new GenericIdentity("a name"), new string[] {"Admin"});
Assert.That(principal.Identity.IsAuthenticated.isTrue(), "IsAuthenticated should be false");
Assert.That(principal.IsInRole("Admin").isTrue(), "IsInRole(\"Admin\") should be true");
System.Threading.Thread.CurrentPrincipal = principal;
var value = protectedByDemandWithNoAuthentication();
return "Note: the fact that IsAuthenticateds is true also makes no difference";
}
catch(Exception ex)
{
Assert.Fail("a security excption was not expected");
return "fail";
}
}
示例10: determineRolesButton_Click
private void determineRolesButton_Click(object sender, System.EventArgs e)
{
if (this.identity != null)
{
this.Cursor = Cursors.WaitCursor;
this.rolesResultsTextBox.Text = "";
this.rolesResultsTextBox.Update();
string[] roles = Roles.GetRolesForUser(this.identity.Name);
IPrincipal principal = new GenericPrincipal(this.identity, roles);
if (principal != null)
{
this.DisplayRolesResults(string.Format(Properties.Resources.CheckingRolesMessage, principal.Identity.Name));
this.DisplayRolesResults(string.Format(Properties.Resources.UserRoleMessage, role1, Convert.ToString(principal.IsInRole(role1))));
this.DisplayRolesResults(string.Format(Properties.Resources.UserRoleMessage, role2, Convert.ToString(principal.IsInRole(role2))));
this.DisplayRolesResults(string.Format(Properties.Resources.UserRoleMessage, role3, Convert.ToString(principal.IsInRole(role3))));
this.DisplayRolesResults(string.Format(Properties.Resources.UserRoleMessage, role4, Convert.ToString(principal.IsInRole(role4))));
}
this.Cursor = Cursors.Arrow;
}
else
{
this.DisplayRolesResults(Properties.Resources.NullIdentityMessage);
}
}