gpt4 book ai didi

xamarin - 为什么 Xamarin 中的 Context.User.Identity.Name 为空?

转载 作者:行者123 更新时间:2023-12-02 07:27:45 26 4
gpt4 key购买 nike

我不明白为什么当我尝试从 Xamarin Context.User.Indetity.Name 连接时为空。我需要做什么特别的事情吗?我登录到服务器并且用户已建立连接。之后我使用以下代码:

var Connection = new HubConnection(Url);
_hub = Connection.CreateHubProxy(hubName);
_hub.On(srvEvent, onData);

await Connection.Start();

但我从来没有得到用户名。我做错了什么?

这是服务器的代码:

var name = Context.User.Identity.Name;
Connections.Add(name, Context.ConnectionId);
return base.OnConnected();

当它来自网络应用程序而不是来自 xamarin 应用程序时它才有效。

谢谢!

最佳答案

这是我告诉你的代码。

我正在使用外部 OAuth2 服务器进行身份验证,因此我必须以某种方式将访问 token 传递给 SignalR,因为 SignalR 使用 Web 套接字来回发送消息,我无法在 header 中传递访问 token ,因为这是网络套接字不支持。

我以这种方式将该访问 token 作为查询字符串参数传递(Javascript 客户端)

$.connection.hub.qs = "access_token=" + mytoken;

然后在我的 SignalR 上添加了一个中间件,它接受该查询字符串并使用 Bearer Token 将其作为授权 header 添加到 header 中。这是在我的启动类中完成的

app.UseAuthQSTokenExtractor();

中间件的代码是这个

namespace Owin
{
public static class AuthorizationQSTokenExtractorExtension
{
public static void UseAuthQSTokenExtractor(this IAppBuilder app)
{
app.Use<AuthorizationQsTokenExtractorMiddleware>();
}
}
}


namespace Chat.Middleware
{
public class AuthorizationQsTokenExtractorMiddleware : OwinMiddleware
{

public AuthorizationQsTokenExtractorMiddleware(OwinMiddleware next)
: base(next)
{

}

public override async Task Invoke(IOwinContext context)
{
Debug.WriteLine("signalr-auth-middleware");

string bearerToken = context.Request.Query.Get("access_token");
Debug.WriteLine("signar-bearer: " + bearerToken);
if (bearerToken != null)
{
TokenHelper.DecodeAndWrite(bearerToken);
string[] authorization = { "Bearer " + bearerToken };
context.Request.Headers.Add("Authorization", authorization);
}

await Next.Invoke(context);

}
}

我的启动类看起来像这样

            app.UseCors(CorsOptions.AllowAll);
app.UseAuthQSTokenExtractor();

JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
app.UseIdentityServerBearerTokenAuthentication(
new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["api:idserver"],
RequiredScopes = new[]
{
"chat-hub"
}
});

var hubConfiguration = new HubConfiguration ();
hubConfiguration.EnableDetailedErrors = true;
app.RunSignalR(hubConfiguration);

您可以在上面的代码中看到我告诉 SignalR 使用 Oauth2 服务器,该代码就是这个

app.UseIdentityServerBearerTokenAuthentication(
new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["api:idserver"],
RequiredScopes = new[]
{
"chat-hub"
}
});

完成所有这些设置后,我可以访问我的 Context.User.Identity.Name,如果您想获取其他 IdentityClaim,您可以这样做

var identity = Context.User.Identity as ClaimsIdentity;

我使用上面的代码来获取像这样的 subjectId (userid)

public static string[] GetIdentityClaimsIssSub(HubCallerContext Context)
{
var identity = Context.User.Identity as ClaimsIdentity;


if (identity == null)
return null;

var issuerFromIdentity = identity.FindFirst("iss");
var subFromIdentity = identity.FindFirst("sub");

if (issuerFromIdentity == null || subFromIdentity == null)
return null;

return new string[] { issuerFromIdentity.Value, subFromIdentity.Value };
}

希望对你有帮助

关于xamarin - 为什么 Xamarin 中的 Context.User.Identity.Name 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38445601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com