大佬教程收集整理的这篇文章主要介绍了除了 OpenId 和 OpenIddict 之外,还允许基本授权,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经正确设置了 OpenIDDict,并且运行良好。
现在,我需要与之交互的应用程序之一只能使用 Authorization 标头执行 POST someurl。我无法控制此应用。
我尝试向我的服务器添加另一条路由:
[EnableCors]
[httpPost("TokensDev"),Produces("application/Json")]
public async Task<IActionResult> ExchangeDev()
{
if (! this.request.headers.TryGetValue("Authorization",out var authheader_))
return Unauthorized();
var authheader = AuthenticationheaderValue.Parse(authheader_);
var credentialBytes = Convert.FromBase64String(authheader.Parameter);
var credentials = EnCoding.UTF8.GetString(credentialBytes).Split(new[] { ':' },2);
var user = await _useRMANager.FindBynameAsync(credentials[0]);
if (user == null)
return Unauthorized();
var result = await _signInManager.checkpasswordSignInAsync(user,credentials[1],lockoutOnFailure: truE);
if (!result.Succeeded)
return Unauthorized();
var principal = await _signInManager.createuserPrincipalAsync(user);
var ticket = new AuthenticationTicket(principal,new AuthenticationPropertIEs(),OpenIDDictServerDefaults.AuthenticationscheR_562_11845@E);
foreach (var claim in ticket.Principal.Claims)
{
claim.SetDesTinations(GetDesTinations(claim,ticket));
}
return SignIn(ticket.Principal,ticket.PropertIEs,ticket.AuthenticationscheR_562_11845@E);
}
但是,问题是 OpenIDDict 阻止了:
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMIDdleware[1]
An unhandled exception has occurred while execuTing the request.
system.invalIdoperationException: An authorization or token response cAnnot be returned from this endpoint.
at AspNet.Security.openIDConnect.Server.openIDConnectServerHandler.SignInAsync(AuthenticationTicket ticket)
at Microsoft.AspNetCore.Authentication.Authenticationservice.SignInAsync(httpContext context,String scheR_562_11845@e,ClaimsPrincipal principal,AuthenticationPropertIEs propertIEs)
at Microsoft.AspNetCore.Mvc.SignInResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.InvokeResultAsync(IActionResult result)
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.InvokeNextResultFilterasync[TFilter,TFilterasync]()
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.Rethrow(ResultExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.ResultNext[TFilter,TFilterasync](State& next,Scope& scope,Object& state,Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.InvokeResultFilters()
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.InvokeNextresourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.Rethrow(resourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.Next(State& next,Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.resourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.builder.RouterMIDdleware.Invoke(httpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMIDdleware.Invoke(httpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMIDdleware.Invoke(httpContext context)
有没有办法配置 OpenIDDict 以允许这样做?
注意:我尝试直接对 OpenID 和 Basic Authorization 使用相同的方法,但是在这种情况下 OpenIDDict 拒绝 CORS 协商...
在深入研究 OpenIdDict 代码数小时后,我找到了一种方法来做我需要的事情。
我不认为它很漂亮,但它有效:
[EnableCors]
[httpPost("TokensDev"),Produces("application/json")]
public async Task<IActionResult> ExchangeDev()
{
if (! this.request.Headers.TryGetValue("Authorization",out var authHeader_))
return Unauthorized();
var authHeader = AuthenticationHeaderValue.Parse(authHeader_);
var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
var credentials = Encoding.UTF8.GetString(credentialBytes).Split(new[] { ':' },2);
var request = new openIdConnectrequest
{
GrantType = "password",Username = credentials[0],password = credentials[1],Scope = "openid",};
request.SetProperty(OpenIdConnectConstants.Properties.messageType,OpenIdConnectConstants.messageTypes.Tokenrequest);
httpContext.SetOpenIdConnectrequest(request);
return await Exchange(); //this is the "normal" openid entry point
}
请注意,这仅是因为第 3 方应用程序正在使用 JSON 结果的 access_token 属性...
以上是大佬教程为你收集整理的除了 OpenId 和 OpenIddict 之外,还允许基本授权全部内容,希望文章能够帮你解决除了 OpenId 和 OpenIddict 之外,还允许基本授权所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。