Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JWT身份验证返回NULL的SignalR Core Android客户端_Java_Android_Asp.net Core_Jwt_Signalr - Fatal编程技术网

Java JWT身份验证返回NULL的SignalR Core Android客户端

Java JWT身份验证返回NULL的SignalR Core Android客户端,java,android,asp.net-core,jwt,signalr,Java,Android,Asp.net Core,Jwt,Signalr,我正在构建一个android应用程序,它可以与asp.net core signalR hub进行通信,没有身份验证,一切都很好,实际上我想不出来。在Web APi中,我使用JWT进行身份验证,并将其作为访问令牌发送回android设备,那么如何将令牌发送到集线器?? 我在文档中发现此代码: HubConnection hubConnection = HubConnectionBuilder.create("https://example.com/myhub") .withAccessToken

我正在构建一个android应用程序,它可以与asp.net core signalR hub进行通信,没有身份验证,一切都很好,实际上我想不出来。在Web APi中,我使用JWT进行身份验证,并将其作为访问令牌发送回android设备,那么如何将令牌发送到集线器?? 我在文档中发现此代码:

HubConnection hubConnection = HubConnectionBuilder.create("https://example.com/myhub")
.withAccessTokenProvider(Single.defer(() -> {
    // Your logic here.
    return Single.just("An Access Token");
})).build();
但是我想不出来!!我应该做的逻辑是什么?? 这也是我的提供者类

public class MyCustomProvider : IUserIdProvider
{
    public string GetUserId(HubConnectionContext connection)
    {
        //throw new NotImplementedException();
        return connection.User?.FindFirst(ClaimTypes.Email).Value?.ToString();
    }
}
这是我的中心

public class LocationHub : Hub
{
    private readonly UserManager<ApplicationUser> _userManager;

    public ApplicationDbContext _Context { get; }

    public LocationHub(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
    {
        _Context = context;
        _userManager = userManager;
    }

    public async Task ShareLocation(double Latitude , double Longitude)
    {
        Console.WriteLine("New location from: "+Context.UserIdentifier+"::"+ Latitude + "///" + Longitude);
        await Clients.Others.SendAsync("ReceiveNewLocation", Latitude, Longitude);
    }

    public override  Task OnConnectedAsync()
    {
        var user = _Context.Users.Where(u => u.Email == Context.UserIdentifier).FirstOrDefault();
        user.LoginStatus = true;
        _Context.SaveChanges();
        return base.OnConnectedAsync();
    }

最后找到了解决方案,我会把它放在这里,以防任何人有这个问题:

PreferencesStore.loadPreferences(this);
    String mToken = PreferencesStore.getToken();
    hubConnection = HubConnectionBuilder.create("http://myserver/locationhub")
            .withHeader("Authorization", mToken)
            .build();

它与文档不同。就在这里,我使用了
.withHeader(“Authorization”,mToken)
,但它工作得很好。

对我来说很有用。非常感谢。
PreferencesStore.loadPreferences(this);
    String mToken = PreferencesStore.getToken();
    hubConnection = HubConnectionBuilder.create("http://myserver/locationhub")
            .withHeader("Authorization", mToken)
            .build();