gpt4 book ai didi

asp.net-mvc - 在 Asp.Net Identity 中设置 redirect_uri

转载 作者:行者123 更新时间:2023-11-30 05:15:58 26 4
gpt4 key购买 nike

我正在尝试使用 Asp.Net Identity 为 facebook 登录设置 redirect_uri。但是,AccountController 中的 GetExternalLogin REST 方法仅在 redirect_uri 为“/”时才会触发。如果我添加任何其他内容,它不会触发 GetExternalLogin,浏览器只会显示 error: invalid_request

然而,url 包含重定向参数,因为它应该例如如果我将 redirect_uri 添加为 http://localhost:25432/testing

响应 URL 如下所示:

http://localhost:25432/api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A25432%2Ftesting&state=0NctHHGq_aiazEurHYbvJT8hDgl0GJ_GGSdFfq2z5SA1

并且浏览器窗口显示:error: invalid_request
知道为什么这只在重定向到“/”而不是任何其他 url 时有效吗?

最佳答案

对于可能遇到此问题的任何其他人:问题是当您从 Visual Studio SPA 模板中获取(复制)ApplicationOAuthProvider.cs 并且它位于此代码所在的位置时:

public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
{
if (context.ClientId == _publicClientId)
{
var expectedRootUri = new Uri(context.Request.Uri, "/");

if (expectedRootUri.AbsoluteUri == context.RedirectUri)
{
context.Validated();
}
}

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

这显然会阻止任何看起来不像 http://localhost/http://example.com/redirect_uri > 因此,例如 http://example.com/home 将不起作用。

下面是 Katana 中 InvokeAuthorizeEndpointAsync 的源代码,它完成了所有工作,您可以看到它调用了可能为此 MVC 注册的任何自定义 OAuthProvider Web API 应用程序(此注册通常发生在 Startup.Auth.cs 中):

private async Task<bool> InvokeAuthorizeEndpointAsync()
{
var authorizeRequest = new AuthorizeEndpointRequest(Request.Query);

var clientContext = new OAuthValidateClientRedirectUriContext(
Context,
Options,
authorizeRequest.ClientId,
authorizeRequest.RedirectUri);

if (!String.IsNullOrEmpty(authorizeRequest.RedirectUri))
{
bool acceptableUri = true;
Uri validatingUri;
if (!Uri.TryCreate(authorizeRequest.RedirectUri, UriKind.Absolute, out validatingUri))
{
// The redirection endpoint URI MUST be an absolute URI
// http://tools.ietf.org/html/rfc6749#section-3.1.2
acceptableUri = false;
}
else if (!String.IsNullOrEmpty(validatingUri.Fragment))
{
// The endpoint URI MUST NOT include a fragment component.
// http://tools.ietf.org/html/rfc6749#section-3.1.2
acceptableUri = false;
}
else if (!Options.AllowInsecureHttp &&
String.Equals(validatingUri.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase))
{
// The redirection endpoint SHOULD require the use of TLS
// http://tools.ietf.org/html/rfc6749#section-3.1.2.1
acceptableUri = false;
}
if (!acceptableUri)
{
clientContext.SetError(Constants.Errors.InvalidRequest);
return await SendErrorRedirectAsync(clientContext, clientContext);
}
}

await Options.Provider.ValidateClientRedirectUri(clientContext);

if (!clientContext.IsValidated)
{
_logger.WriteVerbose("Unable to validate client information");
return await SendErrorRedirectAsync(clientContext, clientContext);
}

var validatingContext = new OAuthValidateAuthorizeRequestContext(
Context,
Options,
authorizeRequest,
clientContext);

if (string.IsNullOrEmpty(authorizeRequest.ResponseType))
{
_logger.WriteVerbose("Authorize endpoint request missing required response_type parameter");
validatingContext.SetError(Constants.Errors.InvalidRequest);
}
else if (!authorizeRequest.IsAuthorizationCodeGrantType &&
!authorizeRequest.IsImplicitGrantType)
{
_logger.WriteVerbose("Authorize endpoint request contains unsupported response_type parameter");
validatingContext.SetError(Constants.Errors.UnsupportedResponseType);
}
else
{
await Options.Provider.ValidateAuthorizeRequest(validatingContext);
}

if (!validatingContext.IsValidated)
{
// an invalid request is not processed further
return await SendErrorRedirectAsync(clientContext, validatingContext);
}

_clientContext = clientContext;
_authorizeEndpointRequest = authorizeRequest;

var authorizeEndpointContext = new OAuthAuthorizeEndpointContext(Context, Options);

await Options.Provider.AuthorizeEndpoint(authorizeEndpointContext);

return authorizeEndpointContext.IsRequestCompleted;
}

这是关键:

 await Options.Provider.ValidateClientRedirectUri(clientContext);

因此,您的解决方案是更改 ValidateClientRedirectUri 执行验证的方式 - 如您所见,默认的 SPA 实现非常简单。

有很多人对 SPA 有疑问,主要是因为它缺少任何类型的有用信息,我的意思是对于 ASP.NET Identity 和 OWIN 的东西,以及关于 KnockoutJS 实现中发生的事情。

我希望 Microsoft 能为这些模板提供更全面的文档,因为任何尝试做更复杂的事情的人都会遇到问题。

我在这上面花了几个小时,深入研究 OWIN (Katana) 源代码,认为是上述实现阻止了我的重定向 URI,但事实并非如此,希望也能帮助其他人。

HTH

关于asp.net-mvc - 在 Asp.Net Identity 中设置 redirect_uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693082/

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