我有一个www.api.com和一个www.client.com 所有注册都将在api.com完成,登录将在api.com完成。 client.com只能看到登录表单的用户界面。来自客户端的web api认证
用户登录后,api.com将令牌返回给用户。如何使用令牌访问api.com中其余的webapi?我想访问GetExployeeByID
方法。使用后登录。我保存在该单词在sessionStorage.setItem('token', data.access_token)
API方法
[RoutePrefix("api/Customer")]
public class CustomerController : ApiController
{
List<customer> list = new List<customer>() { new customer {id=1 ,customerName="Marry",age=13},
new customer { id = 2, customerName = "John", age = 24 } };
[Route("GetExployeeByID/{id:long}")]
[HttpGet]
[Authorize]
public customer GetExployeeByID(long id)
{
return list.FirstOrDefault(x=>x.id==id);
}
}
更新1 这是我的AJAX后调用API登录后
function lgetemp() {
$.ajax({
url: 'http://www.azapi.com:81/api/customer/GetExployeeByID/1',
datatype:"json",
type: 'get',
headers: {
"access_token":sessionStorage.getItem("token")
},
crossDomain: true,
success: function (data) {
debugger
alert(data.customerName)
},
error: function (err) {
debugger
alert('error')
}
})
}
将令牌传递回api后。如何对它进行修改?microsft是否为此构建了代码? –
我的朋友是另外一个问题! :-) –
,因为我已经在标题中传递了令牌。我不知道该往哪里继续。也许我的问题标题有些误导 –