多亏了卡梅隆·廷克(Cameron Tinker)的建议,我才能够使它成功。有几件事使我不胜其烦,因此,如果有人遇到相同的情况,我将在这里分享。
如果您使用Okta,则可以通过Okta中间件软件包来完成所有这些操作。您可以仅使用c#OpenID库来完成此操作,但是Okta.AspNetCore库将帮助您解决问题。
首先,您在Web应用程序中注册中间件。 Okta在其网站上有很多此类示例,而且非常简单。
在您的网络应用中,您可以使用它来获取令牌(当然是在用户通过身份验证之后)
await context.HttpContext?.GetTokenAsync("id_token")
通过标准机制,在API调用中将其作为标头的一部分发送:
"Authorization" : "Bearer [token]"
在Web API端,您使用相同的Okta.AspNetCore中间件软件包,然后可以使用[Authorize]装饰控制器以对它们执行身份验证。这是我被绊倒的地方。如果您不是使用Okta中的默认身份验证服务器来,并且已为您的应用程序设置了自定义服务器,则需要在配置中指定它和受众群体:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = OktaDefaults.ApiAuthenticationScheme;
options.DefaultChallengeScheme = OktaDefaults.ApiAuthenticationScheme;
options.DefaultSignInScheme = OktaDefaults.ApiAuthenticationScheme;
})
.AddOktaWebApi(new OktaWebApiOptions()
{
OktaDomain = oktaDomain,AuthorizationServerId = authServerId,Audience = clientId
});
services.AddAuthorization();
我已经完全忘记了受众部分-通过令牌验证的工作方式,该部分是必需的。
从那里开始,中间件负责为您填充ClaimsPrincipal,因此您可以通过ClaimsPrincipal(HttpContext.User)访问用户信息。我最终创建了一个“ CurrentUserService”,并将其拉入其自己的库中,以便在那里整合所有身份验证处理程序。从而允许我的Web应用程序和Web API代码以相同的方式检查权限并检索有关当前用户的信息。如果您有兴趣,可以在这里找到该代码:
public interface ICurrentUserService
{
public ClaimsPrincipal GetCurrentUser();
public string GetCurrentUserDisplayName();
public string GetCurrentUserFullName();
public string GetCurrentUserId();
public DateTime? GetCurrentUserDob();
public string GetCurrentUserGender();
public AddressFromClaimsDTO GetCurentUserAddress();
public bool IsAuthenticated();
}
public class CurrentUserService : ICurrentUserService
{
private const string FULL_ADDRESS_CLAIM_TYPE = "address";
private readonly IHttpContextAccessor _context;
public CurrentUserService(IHttpContextAccessor context)
{
_context = context;
}
/// <summary>
/// Gets whether or not the current user context is authenticated.
/// </summary>
/// <returns></returns>
public bool IsAuthenticated()
{
return GetCurrentUser().Identity.IsAuthenticated;
}
/// <summary>
/// Gets the current user's address.
/// TODO: tie this into our address data model... but if addresses live in Okta what does that mean?
/// </summary>
/// <returns></returns>
public AddressFromClaimsDTO GetCurentUserAddress()
{
var addressClaim = GetClaim(FULL_ADDRESS_CLAIM_TYPE);
if (addressClaim != null)
{
//var parseValue = addressClaim.Value.ToString().Replace("{address:","{\"address\":");
var address = JsonSerializer.Deserialize<AddressFromClaimsDTO>(addressClaim.Value.ToString());
return address;
}
else
{
return new AddressFromClaimsDTO();
}
}
public ClaimsPrincipal GetCurrentUser()
{
return _context.HttpContext.User;
}
public string GetCurrentUserDisplayName()
{
return GetCurrentUser().Identity.Name;
}
public string GetCurrentUserFullName()
{
throw new NotImplementedException();
}
public string GetCurrentUserId()
{
throw new NotImplementedException();
}
public DateTime? GetCurrentUserDob()
{
var claim = GetClaim("birthdate");
if (claim != null && !string.IsNullOrEmpty(claim.Value))
{
return DateTime.Parse(claim.Value);
}
else
{
return null;
}
}
public string GetCurrentUserGender()
{
return GetClaim("gender")?.Value.ToString();
}
public Claim GetClaim(string claimType)
{
return _context.HttpContext.User.FindFirst(x => x.Type == claimType);
}
}
,
在这种情况下,您的ID提供商Okta将颁发OpenID Connect授权承载令牌,您需要将其传递给要保护的任何应用程序。
在应用程序的Web Api端,您将需要注册中间件来处理Okta的OpenID Connect令牌处理。然后,您可以使用@wip
Scenario Outline: Browse to Target URL having multiple specified WebElements
Given Using browser "<theBrowser>"
When targetURL "<targetURL>" is entered
Then Expected Page "<targetURL>" is loaded
And Loaded Page displays webelements with locator "<locatorID>"
Examples:
| theBrowser | targetURL | locatorID |
| chrome | www.qaweb.net/ | //*[@id="loginwithajaxwidget-5"] |
| firefox | www.ganbatte.net/ | //*[@id="loginwithajaxwidget-2"] |
装饰控制器/操作,并可以检查身份声明。
本文链接:https://www.f2er.com/2789000.html