1

我尝试使用单个身份验证将ASP.NET单页应用程序连接到ASP.NET Web API 2.0服务器。我能够使用提琴手和原始的HTTP请求的服务器进行身份验证如图所示在下面的教程:未能将ASP.NET Web客户端连接到Web API 2.0个人帐户身份验证服务器

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.NE T应用程序在其他地方运行。

+0

如果我正确地理解了你,你问你如何在你的asp.net应用程序中使用web api 2.0服务? – VsMaX

+0

是的,但是来自Visual Studio的“ASP.NET单页应用程序”模板。这意味着我在模板中的哪个位置设置客户端Uri,我不想重写模板生成的所有代码只是想使用它来挂接Web API 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中。

+0

您的答案适合.NET客户端。 –

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给你的服务器端和客户端设置。

相关问题