【开发日志】2014年10月9日

1. 写一个web程序,一个webapi程序,webapi部署在虚拟目录下。 OK 2014-10-9 10:54:50


Index.cshtml

<script>

    $(document).ready( function() {
        $.getJSON("localhost:4040/api/messages")
            .done(function (data) {
                alert(data);
            })
            .fail(function () {
                alert('error!');
            });
    });

</script>



把routeTemplate中的api前缀去掉

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );



    public class MessagesController : ApiController
    {
        public string Get()
        {
            return "Hello World!";
        }
    }



#if DEBUG
    [EnableCors(origins: "*", headers: "*", methods: "*")]
#endif






2. access token



Hello,

I'm trying out IdentityServer and I'm stuck on this scenario. I'm using the MVC OWIN Client and I'm fine with the flow implemented there, except that I'd like to build a single page application and thus I would like to also use the token in Ajax requests I issue from the browser. Using the Implicit Javascript client is a bit too much token handling code on the client side for my taste and I'd prefer to carry out the flow on the server and make the token available to the client. I captured the token using notifications:

Notifications = new OpenIdConnectAuthenticationNotifications
{
    SecurityTokenReceived = async n =>
    {
        var token = n.SecurityToken;

        if (!string.IsNullOrEmpty(token))
        {
            n.OwinContext.Set<string>("idsrv:token", token);
        }
    },
    SecurityTokenValidated = async n =>
        {

            var token = n.OwinContext.Get<string>("idsrv:token");

            if (!string.IsNullOrEmpty(token))
            {
                n.AuthenticationTicket.Identity.AddClaim(
                    new Claim("access_token", token));
            }
        }
}

and then I'm also making the access token available to the HTML page. I also enabled OWIN CORS in the Web API sample and finally I'm trying to call the Web API app from the client, like this, where token is the token is put in the claims before as "access_token":

$.ajax('@Constants.AspNetWebApiSampleApi' + 'identity', {
    headers: { 'authorization': 'Bearer ' + token },
    accepts: 'application/json',
    success: function(data) {
        console.log('data from web api request', data);
    }
});

Unfortunately this doesn't seem to work as I always get a 401 from the Web API.

Any idea about what I might be doing wrong?



发布debug版本的api
Copy-Item -Force -Recurse -Verbose G:\Work2\API\WordoorAPI\bin\* H:\DoorAPI\bin



需要仔细阅读

http://stackoverflow.com/questions/978061/http-get-with-request-body



javascript object  !== json



3. 添加评论后,需要返回评论和转发(如果是转发的话)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值