使用cURL,我们可以使用HTTP Web请求传递用户名,如下所示:
$ curl -u <your_username> https://api.github.com/user
Run Code Online (Sandbox Code Playgroud)
该-u标志接受用于身份验证的用户名,然后cURL将请求密码.cURL示例用于使用GitHub Api进行基本身份验证.
我们如何同样传递用户名和密码以及Invoke-WebRequest?最终目标是在GitHub API中使用基本身份验证的PowerShell.
Notes来自客户端的Wikipedia on Basic Auth.
将用户名和密码合并为一个字符串 username:password
$user = "shaunluttin"
$pass = "super-strong-alpha-numeric-symbolic-long-password"
$pair = "${user}:${pass}"
Run Code Online (Sandbox Code Playgroud)
将字符串编码为Base64的RFC2045-MIME变体,但不限于76个字符/行.
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
Run Code Online (Sandbox Code Playgroud)
创建Auth值作为方法,空格,然后编码对 Method Base64String
$basicAuthValue = "Basic $base64"
Run Code Online (Sandbox Code Playgroud)
创建标题 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
$headers = @{ Authorization = $basicAuthValue }
Run Code Online (Sandbox Code Playgroud)
调用Web请求
Invoke-WebRequest -uri "https://api.github.com/user" -Headers $headers
Run Code Online (Sandbox Code Playgroud)
感谢@briantist的帮助!
PowerShell版本比cURL版本更冗长.这是为什么?@briantist指出GitHub打破了RFC并且PowerShell坚持使用它.这是否意味着cURL也打破了标准?
给定以下JSON对象,
form = {
"name": "",
"address": {
"street": "",
"city": "",
"province": "",
"postalCode": "",
"country": ""
},
"phoneDay": "",
"phoneCell": "",
"businessName": "",
"website": "",
"email": ""
}
Run Code Online (Sandbox Code Playgroud)
什么是自动生成以下C#类的工具?
public class ContactInfo
{
public string Name { get; set; }
public Address Address { get; set; }
public string PhoneDay { get; set; }
public string PhoneCell { get; set; }
public string BusinessName { get; set; }
public string Website { get; set; }
public string Email …Run Code Online (Sandbox Code Playgroud) 似乎%操作在管道之后启动脚本块,尽管about_Script_Blocks表示%不是必需的.
这一切都很好.
get-childitem | %{ write-host $_.Name }
{ write-host 'hello' }
%{ write-host 'hello' }
Run Code Online (Sandbox Code Playgroud)
但是当我们在管道之后添加一个脚本块时,我们需要首先使用%.
get-childitem | { write-host $_.Name }
Run Code Online (Sandbox Code Playgroud) 我们如何在安全的ApiController操作中获取当前用户,而不将userName或userId作为参数传递?
我们假设这是可用的,因为我们处于安全行动中.处于安全行动意味着用户已经过身份验证,并且请求具有她的承载令牌.鉴于WebApi已授权用户,可能存在一种内置方式来访问userId,而不必将其作为操作参数传递.
这里发生了什么?
这是我的指示:
app.directive('submitRequired', function (objSvc) {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
// do something
}
};
});
Run Code Online (Sandbox Code Playgroud)
以下是使用中的指令示例:
<input submit-required="true"></input>
Run Code Online (Sandbox Code Playgroud)
这是实际的错误文本:
Error: [$compile:ctreq] Controller 'ngModel', required by directive 'submitRequired', can't be found!
http://errors.angularjs.org/1.2.2/$compile/ctreq?p0=ngModel&p1=submitRequired
at http://www.domain.ca/Scripts/angular/angular.js:78:12
at getControllers (http://www.domain.ca/Scripts/angular/angular.js:5972:19)
at nodeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:6139:35)
at compositeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5550:15)
at nodeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:6132:24)
at compositeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5550:15)
at publicLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5458:30)
at http://www.domain.ca/Scripts/angular/angular.js:1299:27
at Scope.$get.Scope.$eval (http://www.domain.ca/Scripts/angular/angular.js:11634:28)
at Scope.$get.Scope.$apply (http://www.domain.ca/Scripts/angular/angular.js:11734:23) <input submit-required="true"> angular.js:9159
(anonymous function) angular.js:9159
$get angular.js:6751
nodeLinkFn angular.js:6141
compositeLinkFn angular.js:5550
nodeLinkFn …Run Code Online (Sandbox Code Playgroud) 假设我忽略了一个目录,但我想在其中取消签名特定的子目录.所以我有设置:
/uploads/
/uploads/rubbish/
/uploads/rubbish/stuff/KEEP_ME/
/uploads/foo/
/uploads/foo/bar/lose/
Run Code Online (Sandbox Code Playgroud)
我想忽略除KEEP_ME目录之外的所有内容.我希望忽略看起来像:
/uploads/*
!/uploads/rubbish/stuff/KEEP_ME/
Run Code Online (Sandbox Code Playgroud)
但这不起作用,也不是同一主题的几种排列.
一个有用的是
/uploads/**/**/**/
!/uploads/rubbish/stuff/KEEP_ME/
Run Code Online (Sandbox Code Playgroud)
但这感觉有点限制和冗长?
我尝试按照http://enable-cors.org/server_aspnet.html中的步骤 使我的RESTful API(使用ASP.NET WebAPI2实现)与跨源请求(CORS Enabled)一起工作.除非我修改web.config,否则它无法正常工作.
我安装了WebApi Cors依赖:
install-package Microsoft.AspNet.WebApi.Cors -ProjectName MyProject.Web.Api
Run Code Online (Sandbox Code Playgroud)
然后在我App_Start的课程中我得到WebApiConfig如下:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var corsAttr = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsAttr);
var constraintsResolver = new DefaultInlineConstraintResolver();
constraintsResolver.ConstraintMap.Add("apiVersionConstraint", typeof(ApiVersionConstraint));
config.MapHttpAttributeRoutes(constraintsResolver);
config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));
//config.EnableSystemDiagnosticsTracing();
config.Services.Replace(typeof(ITraceWriter), new SimpleTraceWriter(WebContainerManager.Get<ILogManager>()));
config.Services.Add(typeof(IExceptionLogger), new SimpleExceptionLogger(WebContainerManager.Get<ILogManager>()));
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
}
}
Run Code Online (Sandbox Code Playgroud)
但之后我运行应用程序,我请求Fiddler的资源,如: http:// localhost:51589/api/v1/persons ,在响应中,我看不到我应该看到的HTTP头,例如:
Access-Control-Allow-Methods: POST, PUT, DELETE, GET, OPTIONSAccess-Control-Allow-Origin: *我错过了一步吗?我在控制器上尝试了以下注释:
[EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
相同的结果,没有启用CORS. …
我刚刚在IIS中停止了一个应用程序池.当试图启动它时,IIS抱怨说,
该服务此时无法接受控制消息.(来自HRESULT的异常:0x80080425).
是什么赋予了?从哪里出现这个错误?
查看事件查看器>系统显示以下警告:
为应用程序池"MyAppPool"提供服务的工作进程"1456"未能在指定的时间内停止协议"http"的侦听器通道.数据字段包含错误编号.
为应用程序池"MyAppPool"提供服务的进程与Windows进程激活服务发生致命的通信错误.进程ID为"10592".数据字段包含错误编号.
在关闭期间,为应用程序池"MyAppPool"提供服务的进程超出了时间限制.进程ID为"10516".
这在大约5分钟后解决了,此时我们尝试重新启动网站,并收到:
万维网发布服务(W3SVC)已停止.除非万维网发布服务(W3SVC)正在运行,否则无法启动网站.
因此,我们启动了W3SVC服务,然后我们可以启动我们的网站.
介绍问题
我想知道我们是使用Web API 1还是2.我自己的packages.config表示版本5.0.0.是Web API 1还是2?
搜索和研究
我试过谷歌搜索以下内容:
"web api 1""web api 2"
和
ASP.NET Web API发布历史记录
和
ASP.NET Web API nuget
最好的参考似乎是这两个nuget页面:
是对的吗?我怎么知道?