小编Lar*_*sen的帖子

mvc和webapi之间的身份验证(单独的域/应用程序)

我正在为以下场景寻找好的想法/资源/实现

MVC网站http://mywebsite.com

http://myapi.com上的 Webapi REST服务

重要信息 - 请注意单独的域/应用程序..

用户登录网站,并通过JSONP/CORS从API获取数据

显然,我不希望用户使用基本身份验证在webapi上进行身份验证.但API也暴露于Android/IOS应用程序,所以我需要基本的身份验证

我已经考虑过从MVC站点返回一个令牌,然后在webapi站点写一个DelegatingHandler来使用该令牌进行身份验证,但我想要一些输入,或者甚至更好的解决方案

我做了一个漂亮的图表只是为了掩盖:

图

c# authentication asp.net-mvc cross-domain asp.net-web-api

16
推荐指数
1
解决办法
3924
查看次数

获取请求的URL或操作参数i MediaTypeFormatter.ReadFromStreamAsync

我有一个自定义的WebApi应用程序 MediaTypeFormatter

根据"name"参数(或由此部分URL),应用程序应将请求主体格式化为不同的类型.

这是行动

// http://localhost/api/fire/test/ 
// Route: "api/fire/{name}",

public HttpResponseMessage Post([FromUri] string name, object data)
{
    // Snip
}
Run Code Online (Sandbox Code Playgroud)

这是自定义的MediaTypeFormatter.ReadFromStreamAsync

public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
    var name = "test"; // TODO this should come from the current request

    var formatter = _httpSelfHostConfiguration.Formatters.JsonFormatter;

    if (name.Equals("test", StringComparison.InvariantCultureIgnoreCase))
    {
        return formatter.ReadFromStreamAsync(typeof(SomeType), readStream, content, formatterLogger);
    }
    else
    {
        return formatter.ReadFromStreamAsync(typeof(OtherType), readStream, content, formatterLogger);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# self-hosting asp.net-web-api

5
推荐指数
1
解决办法
810
查看次数