//#get public static string GetCurrentIdentityName(string getType) { IPrincipal principal = Thread.CurrentPrincipal; var str = principal.GetType().ToString(); if (typeof(WindowsPrincipal).IsAssignableFrom(principal.GetType())) //验证Windows账户标识 { if (getType == "0") { var windowsprinciple = (WindowsPrincipal)principal; str = windowsprinciple.Identity.Name; } else if (getType == "1") { str = new WindowsPrincipal(WindowsIdentity.GetCurrent()).Identity.Name; } else { HttpContext myContext = HttpContext.Current; str = myContext.User.Identity.Name;; } if (!String.IsNullOrEmpty(str)) { str = str.Split('\\')[1]; } } else { var genericprincipal = (GenericPrincipal)principal; str = genericprincipal.Identity.Name; } return(str); }
public static string IPrincipalToString(IPrincipal principal) { if (principal == null) { return "NULL"; } if (typeof(CasPrincipal) == principal.GetType()) { ICasPrincipal casPrincipal = (ICasPrincipal) principal; return string.Format("Type>{0}< Identity[{1}] Assertion[{2}]", principal.GetType().Name, IIdentityToString(casPrincipal.Identity), AssertionToString(casPrincipal.Assertion)); } return string.Format("Type>{0}< Identity[{1}]", principal.GetType().Name, IIdentityToString(principal.Identity)); }
private ActivityPrincipal Convert(IPrincipal principal) { if (principal == null) { return(new ActivityPrincipal()); } var activityPrincipal = principal as ActivityPrincipal; if (activityPrincipal != null) { return(activityPrincipal); } var claimsPrincipal = principal as ClaimsPrincipal; if (claimsPrincipal != null) { //This is wrong but we are not using roles yet var roles = claimsPrincipal.Claims.Select(x => x.Value); var p = new ActivityPrincipal( new ActivityIdentity(claimsPrincipal.Identity.Name, claimsPrincipal.Identity.IsAuthenticated, claimsPrincipal.Identity.AuthenticationType) , roles.ToArray() ); return(p); } throw new NotSupportedException($"Converting {principal.GetType()} not supported"); }
public UserPrincipal(IIdentity identity) { AppDomain currentdomain = Thread.GetDomain(); currentdomain.SetPrincipalPolicy(PrincipalPolicy.UnauthenticatedPrincipal); IPrincipal oldPrincipal = Thread.CurrentPrincipal; Thread.CurrentPrincipal = this; try { if (oldPrincipal.GetType() != typeof(UserPrincipal)) { currentdomain.SetThreadPrincipal(this); } } catch { // failed, but we don't care because there's nothing // we can do in this case } currentIdentity = (UserIdentity)identity; }
private static void AddPrincipalContext(IDictionary exceptionContext, IPrincipal principal, string prefix) { if (exceptionContext == null) throw new ArgumentNullException("exceptionContext"); if (prefix == null) prefix = string.Empty; if (principal != null) { exceptionContext.Add(prefix + ".Principal.TypeName", principal.GetType().FullName); if (principal.Identity == null) { exceptionContext.Add((object)prefix + ".Principal.Identity", (object)"Null"); } else { IIdentity identity = principal.Identity; exceptionContext.Add(prefix + ".Principal.Identity.TypeName", identity.GetType().FullName); exceptionContext.Add(prefix + ".Principal.Identity.AuthType", identity.AuthenticationType); exceptionContext.Add(prefix + ".Principal.Identity.IsAuthenticated", Convert.ToBoolean(identity.IsAuthenticated ? 1 : 0)); exceptionContext.Add(prefix + ".Principal.Identity.Name", identity.Name); } } else exceptionContext.Add(prefix + ".Principal", "Null"); }
static void SerializePrincipal(Thread th, IPrincipal value) { MemoryStream ms = new MemoryStream(); bool done = false; if (value.GetType() == typeof(GenericPrincipal)) { GenericPrincipal gp = (GenericPrincipal)value; if (gp.Identity != null && gp.Identity.GetType() == typeof(GenericIdentity)) { GenericIdentity id = (GenericIdentity)gp.Identity; if (id.Name == "" && id.AuthenticationType == "") { if (gp.Roles == null) { ms.WriteByte(2); done = true; } else if (gp.Roles.Length == 0) { ms.WriteByte(3); done = true; } } else { ms.WriteByte(1); BinaryWriter br = new BinaryWriter(ms); br.Write(gp.Identity.Name); br.Write(gp.Identity.AuthenticationType); string [] roles = gp.Roles; if (roles == null) { br.Write((int)(-1)); } else { br.Write(roles.Length); foreach (string s in roles) { br.Write(s); } } br.Flush(); done = true; } } } if (!done) { ms.WriteByte(0); BinaryFormatter bf = new BinaryFormatter(); try { bf.Serialize(ms, value); } catch {} } th.Internal._serialized_principal = ByteArrayToRootDomain(ms.ToArray()); }
internal IPrincipal RemovePrincipalIfNotSerializable() { IPrincipal principal = this.Principal; if (principal != null && !principal.GetType().IsSerializable) { this.Principal = null; } return(principal); }
public static void showWindowsPrincipal() { AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); IPrincipal principal = Thread.CurrentPrincipal; Console.WriteLine("Current principal: " + principal.GetType()); if (typeof(WindowsPrincipal).IsAssignableFrom(principal.GetType())) { WindowsPrincipal windowsPrinciple = (WindowsPrincipal)principal; Console.WriteLine("Principal identity is: " + windowsPrinciple.Identity.Name); } else { GenericPrincipal genericPrincipal = (GenericPrincipal)principal; Console.WriteLine("Principal identity is: " + genericPrincipal.Identity.Name); } }
static void PrintPrincipalInformation() { IPrincipal curPrincipal = Thread.CurrentPrincipal; if (curPrincipal != null) { Console.WriteLine("Тип: " + curPrincipal.GetType().Name); Console.WriteLine("Имя: " + curPrincipal.Identity.Name); Console.WriteLine("Идентификация: " + curPrincipal.Identity.IsAuthenticated); Console.WriteLine(); } }
public static void CheckLiveIdBasicPartialAuthResult(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException("httpContext"); } IPrincipal user = httpContext.User; if (user != null && user.Identity != null && user.GetType().Equals(typeof(GenericPrincipal)) && user.Identity.GetType().Equals(typeof(GenericIdentity)) && string.Equals(user.Identity.AuthenticationType, "LiveIdBasic", StringComparison.OrdinalIgnoreCase)) { throw new HttpException(403, string.Format("Unable to resolve identity: {0}", user.Identity.GetSafeName(true))); } }
private static void PrintPrincipalInformation() { IPrincipal curPrincipal = Thread.CurrentPrincipal; if (curPrincipal != null) { Console.WriteLine("Type: " + curPrincipal.GetType().Name); Console.WriteLine("Name: " + curPrincipal.Identity.Name); Console.WriteLine("Authenticated: " + curPrincipal.Identity.IsAuthenticated); Console.WriteLine(); } }
public static string[] GetRoles(this IPrincipal principal) { MethodInfo method = principal.GetType().GetMethod("GetRoles"); if (method != null) { // RolePrincipal (Web Forms) already has a GetRoles() method return((string[])method.Invoke(principal, null)); } // GenericPrincipal and WindowsPrincipal have this private field return((string[])principal.GetPrivateFieldValue("m_roles")); }
static int Main(string[] args) { // Thread.CurrentPrincipal calls AppDomain.CurrentDomain.GetThreadPrincipal() which // contains annotation attributes and will require GenericPrincipal.GetDefaultInstance // after setting UnauthenticatedPrincipal as the PrincipalPolicy AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.UnauthenticatedPrincipal); IPrincipal genericPrincipal = Thread.CurrentPrincipal; if (genericPrincipal.GetType().Name != "GenericPrincipal") { return(-1); } return(100); }
[System.Security.SecurityCritical] // auto-generated internal IPrincipal RemovePrincipalIfNotSerializable() { IPrincipal currentPrincipal = this.Principal; // If the principal is not serializable, we need to // null it out. if (currentPrincipal != null) { if (!currentPrincipal.GetType().IsSerializable) { this.Principal = null; } } return(currentPrincipal); }
static int Main(string[] args) { // Thread.CurrentPrincipal calls AppDomain.CurrentDomain.GetThreadPrincipal() which // contains annotation attributes and will require WindowsPrincipal.GetDefaultInstance // after setting that as the PrincipalPolicy AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); IPrincipal principal = Thread.CurrentPrincipal; if (principal.GetType().Name != "WindowsPrincipal") { return(-1); } return(100); }
private void PrintPrincipalInformation() { IPrincipal curPrincipal = Thread.CurrentPrincipal; if (curPrincipal != null) { Console.WriteLine("Type: " + curPrincipal.GetType().Name); Console.WriteLine("Name: " + curPrincipal.Identity.Name); Console.WriteLine("Authenticated: " + curPrincipal.Identity.IsAuthenticated); Console.WriteLine(); if (curPrincipal.Identity.Name == "DESKTOP-UVC3FPE\\GumBerS") { ProtectedMethod(); } } }
static void SerializePrincipal (Thread th, IPrincipal value) { MemoryStream ms = new MemoryStream (); bool done = false; if (value.GetType () == typeof (GenericPrincipal)) { GenericPrincipal gp = (GenericPrincipal) value; if (gp.Identity != null && gp.Identity.GetType () == typeof (GenericIdentity)) { GenericIdentity id = (GenericIdentity) gp.Identity; if (id.Name == "" && id.AuthenticationType == "") { if (gp.Roles == null) { ms.WriteByte (2); done = true; } else if (gp.Roles.Length == 0) { ms.WriteByte (3); done = true; } } else { ms.WriteByte (1); BinaryWriter br = new BinaryWriter (ms); br.Write (gp.Identity.Name); br.Write (gp.Identity.AuthenticationType); string [] roles = gp.Roles; if (roles == null) { br.Write ((int) (-1)); } else { br.Write (roles.Length); foreach (string s in roles) { br.Write (s); } } br.Flush (); done = true; } } } if (!done) { ms.WriteByte (0); BinaryFormatter bf = new BinaryFormatter (); try { bf.Serialize (ms, value); } catch {} } th.Internal._serialized_principal = ByteArrayToRootDomain (ms.ToArray ()); }
public virtual Principal GetPrincipal(IPrincipal principal, bool throwExceptionIfUnsuccessful) { if (principal == null) { return(null); } var principalInternal = principal as IPrincipalInternal; if (principalInternal != null) { return(principalInternal.Principal); } if (throwExceptionIfUnsuccessful) { throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "The object of type \"{0}\" does not implement \"{1}\".", principal.GetType(), typeof(IPrincipalInternal))); } return(null); }
internal static ClaimsPrincipal AsClaimsPrincipal(this IPrincipal principal) { Guard.Is <ClaimsPrincipal>(principal, $"The passed principal is of the wrong type ({principal.GetType().Name})"); return((ClaimsPrincipal)principal); }
public static string IPrincipalToString(IPrincipal principal, string newLineDelim, string initialLineIndent, int initialLineIndentCount, string dataDelim, bool useDelimAlways) { StringBuilder sb = new StringBuilder(); string lineIndent = GenerateLineIndent(initialLineIndent, initialLineIndentCount); sb.Append(string.Format("{0}User Information:{1}", lineIndent, newLineDelim)); int memberLineIndentCount = initialLineIndentCount + 2; string memberLineIndent = GenerateLineIndent(initialLineIndent, memberLineIndentCount); if (principal == null) { sb.Append(string.Format("{0}UNDEFINED{1}", memberLineIndent, newLineDelim)); } else { sb.Append(string.Format("{0}Type: {1}{2}", memberLineIndent, DataNullEmptyDisplay(principal.GetType().Name, dataDelim, useDelimAlways), newLineDelim)); sb.Append(IIdentityToString(principal.Identity, newLineDelim, initialLineIndent, memberLineIndentCount, dataDelim, useDelimAlways)); if (typeof(CasPrincipal) == principal.GetType()) { memberLineIndentCount += 2; sb.Append(AssertionToString(((CasPrincipal)principal).Assertion, newLineDelim, initialLineIndent, memberLineIndentCount, dataDelim, useDelimAlways)); } } return sb.ToString(); }
} // DoTransitionDispatch public virtual IMessage SyncProcessMessage(IMessage reqMsg) { Message.DebugOut("\n::::::::::::::::::::::::: CrossAppDomain Channel: Sync call starting"); IMessage errMsg = InternalSink.ValidateMessage(reqMsg); if (errMsg != null) { return(errMsg); } // currentPrincipal is used to save the current principal. It should be // restored on the reply message. IPrincipal currentPrincipal = null; IMessage desRetMsg = null; try { IMethodCallMessage mcmReqMsg = reqMsg as IMethodCallMessage; if (mcmReqMsg != null) { LogicalCallContext lcc = mcmReqMsg.LogicalCallContext; if (lcc != null) { currentPrincipal = lcc.Principal; // If the principal is not serializable, we need to // null it out for the duration of the call. if (currentPrincipal != null) { if (!currentPrincipal.GetType().IsSerializable) { lcc.Principal = null; } } } } MemoryStream reqStm = null; SmuggledMethodCallMessage smuggledMcm = SmuggledMethodCallMessage.SmuggleIfPossible(reqMsg); if (smuggledMcm == null) { //*********************** SERIALIZE REQ-MSG **************** // Deserialization of objects requires permissions that users // of remoting are not guaranteed to possess. Since remoting // can guarantee that it's users can't abuse deserialization // (since it won't allow them to pass in raw blobs of // serialized data), it should assert the permissions // necessary before calling the deserialization code. This // will terminate the security stackwalk caused when // serialization checks for the correct permissions at the // remoting stack frame so the check won't continue on to // the user and fail. // We will hold off from doing this for x-process channels // until the big picture of distributed security is finalized. RemotingServices.LogRemotingStage(RemotingServices.CLIENT_MSG_SER); reqStm = CrossAppDomainSerializer.SerializeMessage(reqMsg); } // Retrieve calling caller context here, where it is safe from the view // of app domain checking code LogicalCallContext oldCallCtx = CallContext.SetLogicalCallContext(null); // Call helper method here, to avoid confusion with stack frames & app domains MemoryStream retStm = null; byte[] responseBytes = null; SmuggledMethodReturnMessage smuggledMrm; RemotingServices.LogRemotingStage(RemotingServices.CLIENT_MSG_SEND); try { if (smuggledMcm != null) { responseBytes = DoTransitionDispatch(null, smuggledMcm, out smuggledMrm); } else { responseBytes = DoTransitionDispatch(reqStm.GetBuffer(), null, out smuggledMrm); } } finally { CallContext.SetLogicalCallContext(oldCallCtx); } if (smuggledMrm != null) { ArrayList deserializedArgs = smuggledMrm.FixupForNewAppDomain(); desRetMsg = new MethodResponse((IMethodCallMessage)reqMsg, smuggledMrm, deserializedArgs); } else { retStm = new MemoryStream(responseBytes); RemotingServices.LogRemotingStage(RemotingServices.CLIENT_RET_DESER); Message.DebugOut("::::::::::::::::::::::::::: CrossAppDomain Channel: Sync call returning!!\n"); //*********************** DESERIALIZE RET-MSG ************** desRetMsg = CrossAppDomainSerializer.DeserializeMessage(retStm, reqMsg as IMethodCallMessage); } } catch (Exception e) { Message.DebugOut("Arrgh.. XAppDomainSink::throwing exception " + e + "\n"); try { desRetMsg = new ReturnMessage(e, (reqMsg as IMethodCallMessage)); } catch (Exception) { // Fatal Error .. can't do much here } } // restore the principal if necessary. if (currentPrincipal != null) { IMethodReturnMessage mrmRetMsg = desRetMsg as IMethodReturnMessage; if (mrmRetMsg != null) { LogicalCallContext lcc = mrmRetMsg.LogicalCallContext; lcc.Principal = currentPrincipal; } } RemotingServices.LogRemotingStage(RemotingServices.CLIENT_RET_SINK_CHAIN); return(desRetMsg); }
public static bool IsVistaPrincipal(IPrincipal principal) { return principal.GetType().IsInstanceOfType(typeof(VistaPrincipal)); }
public static bool IsVistaPrincipal(IPrincipal principal) { return(principal.GetType().IsInstanceOfType(typeof(VistaPrincipal))); }