我有客户端应用程序,它在nodejs服务器上运行并具有Web Api项目。我在Startup.cs类中有Bearer Token Authentication:
// Configure the application for OAuth based flow
var oAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
AllowInsecureHttp = true
};
app.UseOAuthAuthorizationServer(oAuthOptions);
app.UseOAuthBearerAuthentication(newOAuthBearerAuthenticationOptions());
我想在客户端启动集线器:
$.connection.hub.url = 'https://localhost:44302/signalr/hubs';
this.proxy = $.connection.chathub;
this.proxy.client.onMessage = function (stock){
};
$.connection.hub.start()
.done(function(error){})
.fail(function(error){});
在客户端,我得到这样的令牌:
$.post("https://localhost:44302/token", { grant_type: "password", username: user.username, password: user.password})
.done(function (data) {
if (data && data.access_token) {
bearerToken = data.access_token;
}
})
.fail(function (xhr) {
error('Wrong password or username.');
});
所以我在bearerToken中保存了令牌,但现在我怎么把它传递给服务器端呢? 在这段代码中:
[HubName("chathub")]
public class ChatHub : Hub
{
public override Task OnConnected()
{
if (Context.User.Identity.IsAuthenticated)
{
var identity = (ClaimsIdentity)Context.User.Identity;
...
IsAuthenticated为false,User中没有数据。