gpt4 book ai didi

html - 如何根据 Web API/Asp.Net Identity 验证 HTML-5 音频下载请求?

转载 作者:太空狗 更新时间:2023-10-29 13:30:28 25 4
gpt4 key购买 nike

我需要从我的 Web API 流式传输音频。在标准 HTML-5 中,音频 src 属性设置为来自 WebAPI 的音频的 URI。

问题是:使用 Asp.Net Identity 保护的 Web API 需要在 header 中传递不记名 token ,但是 HTML AUDIO TAG 不允许我们这样做。我最终被排除在两个选择之外:

Approach 1. Download the HTML using XHR request & play locally.

Approach 2. Pass headers via query string. So that we could inject the token into OWIN pipeline at point of time during request processing.

上面提到的第一种方法是不可行的,因为如果我们在本地下载音频,我们将错过 Web API 提供的流媒体功能。

您能否协助方法 2,即在 Web API 端,我们可以从 URL 读取不记名 token ,然后启动 Asp.Net 身份验证?

最佳答案

创建这个提供者类

public class QueryStringOAuthBearerProvider : OAuthBearerAuthenticationProvider
{
public override Task RequestToken(OAuthRequestTokenContext context)
{
var value = context.Request.Query.Get("access_token");

if (!string.IsNullOrEmpty(value))
{
context.Token = value;
}

return Task.FromResult<object>(null);
}
}

在 Startup.cs 中使用

OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true
};

// Enable the application to use bearer tokens to authenticate users

//app.UseOAuthBearerTokens(OAuthOptions); // old line

app.UseOAuthAuthorizationServer(OAuthOptions); // new line

// Enable the application to retrieve tokens from query string to authenticate users
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
Provider = new QueryStringOAuthBearerProvider()
});

现在它将像这样从 url "..../?access_token=xxxxxxx"获取 token 并尝试验证。

关于html - 如何根据 Web API/Asp.Net Identity 验证 HTML-5 音频下载请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34473724/

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