本文整理汇总了C#中SecurityTokenHandlerCollection.ReadToken方法的典型用法代码示例。如果您正苦于以下问题:C# SecurityTokenHandlerCollection.ReadToken方法的具体用法?C# SecurityTokenHandlerCollection.ReadToken怎么用?C# SecurityTokenHandlerCollection.ReadToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SecurityTokenHandlerCollection
的用法示例。
在下文中一共展示了SecurityTokenHandlerCollection.ReadToken方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToSecurityToken
/// <summary>
/// Turns a supported generic XML security token to a security token.
/// </summary>
/// <param name="token">The generic XML security token.</param>
/// <param name="handler">The security token handler.</param>
/// <returns>A SecurityToken</returns>
public static SecurityToken ToSecurityToken(this GenericXmlSecurityToken token, SecurityTokenHandlerCollection handler)
{
var reader = new XmlTextReader(new StringReader(token.TokenXml.OuterXml));
if (handler.CanReadToken(reader))
{
return handler.ReadToken(reader);
}
else
{
throw new InvalidOperationException("Unsupported token type");
}
}
示例2: LoginWithUsernamePassword
private static void LoginWithUsernamePassword(AuthenticationHeaderValue credentials, SecurityTokenHandlerCollection handlers, HttpApplication context)
{
try
{
var cred = EncodingFactory.ReadFileText(Convert.FromBase64String(credentials.Parameter));
var separator = cred.IndexOf(':');
var name = cred.Substring(0, separator);
var password = cred.Substring(separator + 1);
var manager = new TokenManager(GetServiceName(context), name, password);
SecurityToken token = null;
var xmlSecurityToken = manager.GetToken(HttpContext.Current.Request.Url.ToString()) as GenericXmlSecurityToken;
if (xmlSecurityToken != null)
{
token = handlers.ReadToken(new XmlTextReader(new StringReader(xmlSecurityToken.TokenXml.OuterXml)));
}
var securityToken = handlers.ValidateToken(token);
var principal = new ClaimsPrincipal(securityToken);
var identity = principal.Identity as ClaimsIdentity;
if (identity != null) identity.BootstrapContext = new BootstrapContext(xmlSecurityToken.TokenXml.OuterXml);
Thread.CurrentPrincipal = principal;
context.Context.User = principal;
var sessionToken = new SessionSecurityToken(principal);
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionToken);
}
catch (Exception)
{
context.Response.AppendHeader("X-InvalidCredentials", "basic");
throw;
}
}
示例3: ReadSecurityToken
/// <summary>
/// Reads a <see cref="SecurityToken"/> from the provided XML representation.
/// </summary>
/// <param name="securityTokenXml">The XML representation of the security token.</param>
/// <param name="securityTokenHandlers">The <see cref="SecurityTokenHandlerCollection"/> used to
/// read the token.</param>
/// <returns>A <see cref="SecurityToken"/>.</returns>
protected virtual SecurityToken ReadSecurityToken(XmlElement securityTokenXml,
SecurityTokenHandlerCollection securityTokenHandlers)
{
SecurityToken securityToken = null;
XmlReader reader = new XmlNodeReader(securityTokenXml);
reader.MoveToContent();
securityToken = securityTokenHandlers.ReadToken(reader);
if (securityToken == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID4051, securityTokenXml, reader.LocalName, reader.NamespaceURI)));
}
return securityToken;
}
示例4: LoginWithToken
private static void LoginWithToken(AuthenticationHeaderValue credentials, SecurityTokenHandlerCollection handlers, HttpApplication context)
{
try
{
var token = Convert.FromBase64String(credentials.Parameter);
using (var stream = new MemoryStream(token))
{
using (var xmlReader = XmlReader.Create(stream))
{
var securityToken = handlers.ReadToken(xmlReader);
var identities = handlers.ValidateToken(securityToken);
var principal = new ClaimsPrincipal(identities);
var identity = principal.Identity as ClaimsIdentity;
if (identity != null) identity.BootstrapContext = new BootstrapContext(token);
Thread.CurrentPrincipal = principal;
context.Context.User = principal;
}
}
}
catch (Exception)
{
context.Response.AppendHeader("X-InvalidCredentials", "token");
throw;
}
}