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給你的服務器端和客戶端設置。

相關問題