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"); }
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)"); }
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); }
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)"); }
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")); }
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"); }
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; }); }
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"; } }
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"; } }
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); } }