SignalR-如何使用OpenID Connect的服务器验证.Net控制台应用程序

时间:2016-08-31 23:22:37

标签: c# asp.net signalr signalr-hub signalr.client

我有一台使用Azure AD的现有服务器。我想添加SignalR Hub并从控制台应用程序连接到该集线器。然而它失败了(下面的详细错误)。

来自Fiddler我可以看到我成功收到了访问令牌,但当HubConnection尝试启动时,下一个请求被重定向到登录页面。

我已按照here说明并同时使用了connection.Headers.Add("Authorization", "Bearer " + accessToken)connection.Headers.Add("Bearer", accessToken)

在这种情况下保护和集成SignalR的正确方法是什么?

我试过的客户端代码

        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
{
...
}

现有的auth服务器代码:

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)

0 个答案:

没有答案