【问题标题】:how a mvc web project backend access to another mvc api project一个 mvc web 项目后端如何访问另一个 mvc api 项目
【发布时间】:2016-10-28 11:55:48
【问题描述】:

有两个 asp.net mvc 项目。一个是 mvc api,另一个是 mvc web。 使用owin 进行auth2 的mvc api。和 mvc web 访问支持的 api,而不是 js。 问题是,当 web (backed) 访问 api 时,将没有响应,但如果我使用桌面 win 表单客户端,它可以工作。 我想知道为什么,以及如何解决它。

一些代码和包,vs2015

拥有客户端

private HttpClient _client;
....
    private async Task<TokenParam> GetAccessToken(string user,string psw)
    {
        var parameters = new Dictionary<string,string>();
        parameters.Add("grant_type","password");
        parameters.Add("username",user);
        parameters.Add("password",psw);

        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(Id + ":" + Secret)));

        var response = await _client.PostAsync("/token",new FormUrlEncodedContent(parameters));
        var value = await response.Content.ReadAsStringAsync();-----> here no response back from api server

        if (response.StatusCode == HttpStatusCode.OK)
        {
            var o = value.FromJson<TokenParam>();
            o.LoginTime = DateTime.Now.AddSeconds(-10);
            return o;
        }
        return null;
    }

自己的服务器端

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
        oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name,context.UserName));
        var ticket = new AuthenticationTicket(oAuthIdentity,new AuthenticationProperties());
        context.Validated(ticket);
        await base.GrantResourceOwnerCredentials(context);
    }

我的 packages.config 文件

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net452" />
  <package id="bootstrap" version="3.0.0" targetFramework="net452" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="jQuery" version="1.10.2" targetFramework="net452" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
  <package id="Modernizr" version="2.6.2" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
  <package id="Owin" version="1.0" targetFramework="net452" />
  <package id="Respond" version="1.2.0" targetFramework="net452" />
  <package id="WebGrease" version="1.5.2" targetFramework="net452" />
</packages>

【问题讨论】:

  • 为什么在访问 /token 端点时使用基本身份验证?端点的状态码和响应是什么?

标签: asp.net-mvc api web owin


【解决方案1】:

有错误,当我创建另外两个项目时,它们都运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2012-12-21
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多