How to get Owin identity in Signalr hub

I have an application written in ASP.NET MVC using SignalR with Owin external authentication (steam).

Problem is that I can't obtain any identity information inside SignalR hub. Identity.Name returns empty string, Identity.Claims is empty.

Startup.cs

public class Startup
{
  public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Home/Index"),
            AuthenticationMode = AuthenticationMode.Active

        });

      app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
     app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

        app.UseSteamAuthentication("API KEY");
    }
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        app.MapSignalR();
    }
}

In SteamCallBack

authenticateResult?.Identity.Claims

is not empty, it returns correct Identity.Name provided by Steam.

 public async Task<ActionResult> SteamCallback()
    {
        ....

        var authenticateResult =
               await HttpContext.GetOwinContext().Authentication.AuthenticateAsync("ExternalCookie");
        var firstOrDefault = authenticateResult?.Identity.Claims.FirstOrDefault(claim => claim.Issuer == "Steam" && claim.Type.Contains("nameidentifier"));

     ...
    }

Inside Hub all of the following are null/empty

var z = Context.User.Identity.Name;

var b = Context.User.Identity.AuthenticationType;

var x = ((ClaimsIdentity)Context.User.Identity).Claims.ToList();


I solve my problem. I forgot to sign in user using IAuthenticationManager.SignIn method.

Example use of that method:

var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties()
{
    AllowRefresh = true,
    IsPersistent = true,
    ExpiresUtc = DateTime.UtcNow.AddDays(7)
}, identity);
链接地址: http://www.djcxy.com/p/95108.html

上一篇: 无法从OBD获取VIN号码响应

下一篇: 如何在信号中心获得Owin身份