C# SignalR如何通过使用OpenID Connect的服务器验证.Net控制台应用程序
我有一个使用Azure AD的现有服务器。我想添加SignalR Hub并从控制台应用程序连接到该Hub。但它失败了(详细错误如下) 从Fiddler中,我可以看到我成功地收到了访问令牌,但当HubConnection尝试启动时,下一个请求被重定向到登录页面 我按照说明使用了C# SignalR如何通过使用OpenID Connect的服务器验证.Net控制台应用程序,c#,asp.net,signalr,signalr-hub,signalr.client,C#,Asp.net,Signalr,Signalr Hub,Signalr.client,我有一个使用Azure AD的现有服务器。我想添加SignalR Hub并从控制台应用程序连接到该Hub。但它失败了(详细错误如下) 从Fiddler中,我可以看到我成功地收到了访问令牌,但当HubConnection尝试启动时,下一个请求被重定向到登录页面 我按照说明使用了connection.Headers.Add(“授权”、“承载人”+accessToken)和connection.Headers.Add(“承载人”,accessToken) 在这种情况下,保护和集成信号机的正确方法是什么
connection.Headers.Add(“授权”、“承载人”+accessToken)
和connection.Headers.Add(“承载人”,accessToken)
在这种情况下,保护和集成信号机的正确方法是什么
我试过的客户端代码
HubConnection connection = new HubConnection(webEndpoint);
string accessToken = GetAccessToken().Result;
connection.Headers.Add("Authorization", "Bearer " + accessToken);
connection.Headers.Add("Bearer", accessToken);
IHubProxy webJobHub = connection.CreateHubProxy("JobHub");
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
log.FatalException("Error start SignalR connection", task.Exception.GetBaseException());
}
else
{
log.Info("SignalR connection established");
}
}).Wait();
...
public static async Task<string> GetAccessToken()
{
var authContext = new AuthenticationContext(Authority);
// ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
var clientCredential = new ClientCredential(ClientId, AppKey);
AuthenticationResult result = await authContext.AcquireTokenAsync(AppIdUri, clientCredential);
return result.AccessToken;
}
现有身份验证服务器代码:
[Authorize]
public class JobHub : Hub
{
...
}
public partial class Startup
{
...
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = ClientId,
Authority = Authority,
PostLogoutRedirectUri = PostLogoutRedirectUri,
RedirectUri = PostLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
...
}
});
}
}
实际的异常,基本上它应该接收json,但被重定向到登录页面
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.<>c.<GetNegotiationResponse>b__0_1(String raw)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass31_0`2.<Then>b__0(Task`1 t)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3_0.<RunTask>b__0(Task`1 t)
Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符: