我正在使用AspNetCore TestServer并尝试连接到为NTLM身份验证配置的API服务.我要连接的服务需要访问System.Security.Claims.ClaimsPrincipal.Identity.
从我的控制台应用程序,我可以如下初始化他的HTTPClient,它的工作原理
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true,
PreAuthenticate = true
};
HttpClient client = new HttpClient(handler);
Run Code Online (Sandbox Code Playgroud)
但是,TestServer CreateClient方法不接受HttpClientHandler.那么如何配置使用UseDefaultCredentials?
当我运行它的 Windows 服务有权调用 web 服务时,以下 PowerShell invoke-webrequest 对我有用。然而,情况并非总是如此。
我需要能够使用 Windows 身份验证,但还需要为呼叫设置帐户用户名\密码。有没有人有一些示例代码来执行此操作?
Invoke-WebRequest -UseBasicParsing "url" -UseDefaultCredentials -Method GET
Run Code Online (Sandbox Code Playgroud) 我可以使用类似于以下的代码在 SwaggerUI 中显示多个登录选项
services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Google", new OAuth2Scheme
{
Description = "Google",
Type = "oauth2",
Flow = "implicit",
TokenUrl = "https://www.googleapis.com/oauth2/v3/token",
AuthorizationUrl = "https://accounts.google.com/o/oauth2/auth"
});
c.AddSecurityDefinition("Microsoft", new OAuth2Scheme
{
Description = "Microsoft",
Type = "oauth2",
Flow = "implicit",
TokenUrl = "blah",
AuthorizationUrl = "blah"
});
c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
Description = "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
In = "header",
Name = "Authorization",
Type = "apiKey"
});
c.OperationFilter<SecurityRequirementsOperationFilter>();
Run Code Online (Sandbox Code Playgroud)
和类似的东西
app.UseSwaggerUI(c =>
{
c.OAuthClientId(this.Configuration["Authentication:Google:ClientId"]);
c.OAuthClientSecret(this.Configuration["Authentication:Google:ClientSecret"]);
c.OAuthAppName("blah"); …Run Code Online (Sandbox Code Playgroud) 我有一个aspnetcore Web API,并使用以下代码为Windows Auth配置主机.
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls("http://TestServer:5000")
.UseStartup<Startup>()
.UseWebListener(options =>
{
options.Listener.AuthenticationManager.AuthenticationSchemes = Microsoft.Net.Http.Server.AuthenticationSchemes.NTLM;
//NOTE: Multiple auth assignments don't work in 1.0.0 - Wait for 1.1.* for the line below to start working
//See: https://github.com/aspnet/Announcements/issues/198
//options.Listener.AuthenticationManager.AllowAnonymous = true;
})
.Build();
host.Run();
Run Code Online (Sandbox Code Playgroud)
调用客户端配置如下
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true,
PreAuthenticate = true
};
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("http://TestServer:5000/");
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Run Code Online (Sandbox Code Playgroud)
在我正在呼叫的服务中,我可以访问主叫用户的ClaimsPrincipal身份.
我的问题是如何使用通过CreateClient方法初始化的TestServer Client从集成测试中调用此服务.从集成测试调用时,确保设置标识的最佳方法是什么?
我也在使用XUnit,以防你想知道.
任何帮助或建议将不胜感激.
如何检查appsettings.json.NET Core中是否存在配置节?
即使一个节不存在,以下代码也将始终返回实例化的实例。
例如
var section = this.Configuration.GetSection<TestSection>("testsection");
Run Code Online (Sandbox Code Playgroud) 我为 Azure Devops 编写了一个自定义扩展,其中包含自定义连接服务和构建任务。在通过管道可视化设计器配置任务时,我可以使用连接服务来选择服务,然后使用该服务使用 API 中的数据填充选项列表。
但是,当任务执行时,我如何使用所选的服务。我需要从index.ts 访问该服务。该服务告诉我端点和 API 密钥。
在index.ts中,我可以使用类似以下代码的内容来访问服务的Guid,但是我可以使用Guid来获取服务或其详细信息吗?
import tl = require('azure-pipelines-task-lib/task');
async function run() {
try {
const serviceString: string = tl.getInput('TestService', true);
if (serviceString == 'bad') {
tl.setResult(tl.TaskResult.Failed, 'Bad input was given');
return;
} ...
Run Code Online (Sandbox Code Playgroud)
我已经进行了大量搜索和阅读(包括以下文章),但找不到任何示例。
https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops
https://learn.microsoft.com/en-us/azure/devops/extend/develop/service-endpoints?view=azure-devops
我允许客户通过 Swashbuckle 访问我的 SaaS API。他们需要通过可用授权弹出窗口使用 OAuth 进行身份验证。当他们通过弹出窗口单击授权按钮时,他们需要通过 gmail 进行身份验证。但是,这显示了我需要对最终用户隐藏的 Auth0 client_id 和 client_secret SwashBuckle 使用。
有谁知道是否有办法隐藏它?
我在这个问题上附上了截图。
我在 AddSwaggerGen 中的代码包含以下内容
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "oauth2",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
AuthorizationCode = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri(settings.AuthorityAuthorizeUri),
TokenUrl = new Uri(settings.AuthorityTokenUri),
}
},
Scheme = "oauth2"
});
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "Standard Authorization header using the Bearer scheme. Example: \"Bearer {token}\"",
Name = "Authorization",
In …Run Code Online (Sandbox Code Playgroud) 我在 Octopus Deploy 中运行了以下 Powershell 脚本。
但是,如果它们尚未安装,我只希望它们安装。
我安装了它们,最好它也只会在它们低于某个版本时安装它们。
有人可以建议什么被认为是这样做的最佳方法吗?
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Confirm:$False -Force
Install-Module -Name SqlServer -AllowClobber -Confirm:$False -Force
Run Code Online (Sandbox Code Playgroud) asp.net-core ×5
.net ×2
powershell ×2
swagger ×2
xunit ×2
.net-core ×1
app-config ×1
appsettings ×1
azure ×1
azure-devops ×1
json ×1
oauth-2.0 ×1
rest ×1
sql-server ×1
swagger-ui ×1
swashbuckle ×1
tfs ×1
typescript ×1
web-services ×1