我从ADFS获得JSON Web令牌并成功调用Web API函数。有效。但是,我无法将代码转换为Windows 10 Native或通用应用程序。使用JWT的ADFS和本地应用程序身份验证
任何人都知道该怎么办呢?我的下面的代码在控制台应用程序中完美工作
我没有任何想法如何纳入本机应用程序WSTrustChannelFactory
。另外,下面的(GenericXmlSecurityToken).TokenXml也有问题。
var token = GenericXmlSecurityToken GetJWT();
client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.TokenXml.OuterXml);
或者更适合移动应用的想法?
谢谢
private static GenericXmlSecurityToken GetJWT()
{
//string endpointUri = string.Format("https://{0}/adfs/services/trust/13/usernamemixed", _serverName);
string endpointUri = string.Format("https://{0}/adfs/services/trust/2005/usernamemixed", _serverName);
var factory = new WSTrustChannelFactory(
new UserNameWSTrustBinding(),
new EndpointAddress(endpointUri));
//factory.TrustVersion = TrustVersion.WSTrust13;
factory.TrustVersion = TrustVersion.WSTrustFeb2005;
if (factory.Credentials != null)
{
factory.Credentials.UserName.UserName = _userName;
factory.Credentials.UserName.Password = _password;
}
var rst = new RequestSecurityToken
{
RequestType = RequestTypes.Issue,
KeyType = KeyTypes.Bearer,
AppliesTo = new EndpointReference(_relyingPartyUri),
KeySizeInBits = 0,
TokenType = "urn:ietf:params:oauth:token-type:jwt",
};
var channel = factory.CreateChannel();
try
{
var token = channel.Issue(rst);
return token as GenericXmlSecurityToken;
}
catch (ProtocolException ex)
{
Debug.Write(ex.Message);
}
return null;
}