无法将ASP.NET Web Client连接到Web API 2.0个人帐户身份验证服务器

时间:2013-12-10 19:01:33

标签: asp.net asp.net-mvc asp.net-web-api asp.net-mvc-5 asp.net-4.5

我正在尝试使用个人身份验证将ASP.NET单页面应用程序连接到ASP.NET Web API 2.0服务器。我可以使用Fiddler和原始http请求对服务器进行身份验证,如下面的教程所示:

Individual Accounts in Web API

我的问题是如何将单独的单页客户端示例轻松指向Web API服务?它只是对配置文件或连接字符串的简单更改吗?

令人惊讶的是,我无法在网上找到关于如何做到这一点的大量信息可能是因为它看似简单,而我只是因为我是ASP.NET的新手而错过了一些东西

更新

我基本上希望这指向不同的Web API 2.0服务器:

 static Startup()
    {
        PublicClientId = "self";

        UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());

        OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            AllowInsecureHttp = true
        };
    }


public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Enable the application to use bearer tokens to authenticate users
        app.UseOAuthBearerTokens(OAuthOptions);


....
}

目前指向应用程序的根目录,但Web API 2.0服务器实际上是在另一个运行其他位置的ASP.NET应用程序上。

2 个答案:

答案 0 :(得分:1)

这是向Web Api 2.0发送请求所需的所有代码:

//line below is for supporting self-generated ssl certificates, you can ommit if you're using http
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 
HttpClientHandler handler = new HttpClientHandler();
//this credentials will be in header
handler.Credentials = new NetworkCredential("someLogin", "somepassword");
client = new HttpClient(handler);

client.BaseAddress = new Uri("http://localhost:4567");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
mediaTypeFormatter = new JsonMediaTypeFormatter();
var content = new ObjectContent<T>(item, mediaTypeFormatter);
var response = await client.PostAsync("api/account/register", content);
if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.BadRequest)
{
    throw new ArgumentException("Internal server error");
}
//and here you have successfully sent request and received response from web api.

您可以将其放入某个方法然后使用方法参数传递例如主机Uri。例如,如上所述,您可以将其保存在Web.config中。

答案 1 :(得分:0)

因此,在使用Visual Studio 2013中的新ASP.NET MVC 5和单页应用程序模板后,我发现身份验证和安全性有很大不同。从任何文档中都不是那么明显。

它们都使用“新”ASP.NET标识来替换旧的成员资格但是当您使用ASP.NET MVC 5模板时,您通过以前的控制器获得了相同的旧SQL Server类型的安全设置,使用SPA(单页应用程序)模板,您可以获得基于API的令牌认证。

将SPA连接到位于其他位置的另一个API 2.0的唯一方法是在JavaScript端的客户端,这有点令人困惑,因为这不是ASP.NET MVC 5模板的工作方式。令人困惑的是,SPA为您提供服务器端和客户端设置。