2017-01-01 107 views
22

任何人都可以指向我一些很好的文檔,或提供有關實現ASP.NET核心REST API的身份驗證和授權的最佳方式的好信息。我需要對應用程序進行身份驗證和授權首先驗證並授權用戶。使用ASP.NET核心的應用程序和用戶身份驗證

理想情況下,我希望能夠限制經過身份驗證的應用程序和/或用戶可以訪問的控制器方法。

我想使用AspNet.Security.OpenIdConnect.Serverenter進行應用程序認證,但我不確定如何最好地執行用戶認證。也許重用不同端點上的OpenIdConnect身份驗證,以供具有不同標頭的用戶包含用戶令牌。

一旦通過身份驗證,我想使用角色基本安全來限制哪些控制器方法可以被訪問。

這是解決此問題的正確途徑嗎?

+0

我發現[this](https://samueleresca.net/2016/12/developing-token-authentication-using-asp-net-core/)非常有幫助,不確定它是否是你正在尋找的東西。 –

+0

我會說'無狀態'是要走的路。實質上,您需要在每次請求時發送auth有效內容數據,這將允許您確定請求可以訪問和不可訪問的內容。看到這個:http://www.developerhandbook.com/c-sharp/create-restful-api-authentication-using-web-api-jwt/ – scgough

+0

@邁克爾愛德華我只是在以前同樣的事情賞金,請參閱這個[question](http://stackoverflow.com/q/42121854/1938988)及其答案 –

回答

0

我找不到任何好的文檔,但是我必須實現相同的功能,所以我通過將標準ASP.NET身份驗證模板中的操作修改爲REST API等價物來編寫其餘的api。

比如這裏是我工作過的登錄操作:

// POST: /Account/Login 
    [HttpPost("[action]")] 
    [AllowAnonymous] 
    public async Task<ReturnValue<ApplicationUser>> Login([FromBody] loginModel login) 
    { 
     if (ModelState.IsValid) 
     { 
      ApplicationUser user = await _userManager.FindByEmailAsync(login.email); 

      if (user == null) 
      { 
       return new ReturnValue<ApplicationUser>(false, "Login failed, check username and password.", null); 
      } 
      // else if (user.EmailConfirmed == false) 
      // { 
      //  return new ReturnValue<ApplicationUser>(true, "Confirm email address.", null, user); 
      // } 
      else 
      { 
       // This doesn't count login failures towards account lockout 
       // To enable password failures to trigger account lockout, set lockoutOnFailure: true 
       var result = await _signInManager.PasswordSignInAsync(user, login.password, (bool)login.rememberMe, lockoutOnFailure: false); 
       if (result.Succeeded) 
       { 
        return new ReturnValue<ApplicationUser>(true, user); 
       } 
       //if (result.RequiresTwoFactor) 
       //{ 
       // return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
       //} 
       if (result.IsLockedOut) 
       { 
        return new ReturnValue<ApplicationUser>(false, "The account is locked out.", null); 
       } 
      } 
     } 
     else 
     { 
      string message = string.Join("; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); 
      return new ReturnValue<ApplicationUser>(false, "Invalid login attempt: " + message, null); 
     } 

     // If we got this far, something failed in the model. 
     return new ReturnValue<ApplicationUser>(false, "Login failed.", null); 
    } 

如果調用瀏覽器中通過JavaScript API的餅乾將被加載,你應該能夠作出進一步的授權調用的API,如果您從其他類型的客戶端進行調用,則需要確保CookieContainer保留用於授權調用。

從這一點你可以使用[授權]裝飾通過標準的Microsoft庫授權您的REST API控制器:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity

好運。

6

這實際上是一個艱難的問題是,它似乎因爲正在使用的API客戶端(軟件客戶端)的類型,似乎開車需要什麼樣的認證*。例如,在Web應用程序中,Web應用程序需要身份驗證*,則Asp.Net Identity可以使用令牌或Cookie。但是,如果其他客戶要使用所提供的服務(移動應用程序,WUP應用程序),那麼使用令牌認證可能會更容易實現。當我遇到這個問題時,我碰到了我有一個知識缺口的問題,因爲我沒有「T真正理解OAuth的,我必須回到基礎。

https://alexbilbie.com/guide-to-oauth-2-grants/

https://www.pluralsight.com/courses/oauth2-json-web-tokens-openid-connect-introduction

大多數圍繞的ASP教程。網絡身份「似乎」面向網絡客戶端。雖然有可能找到那些沒有的。隨着asp.net內核的引入,語法已經發生了變化,許多顯示結合cookie和令牌認證的舊教程不再適用。另外,Web Api不再與Visual Studio中的其他項目類型分開,使得更改變得更加明顯。以下是一些較老的教程。

http://satvasolutions.com/combine-asp-net-identity-web-api-and-mvc-best-in-a-single-web-app/

http://blog.iteedee.com/2014/03/asp-net-identity-2-0-cookie-token-authentication/

Combine the use of authentication both for MVC pages and for Web API pages?

IdentityServer是一個完全有效的解決方案,包括客戶端憑證和資源所有者憑證授予(用戶名,密碼)的作品和Brock阿倫平時一直非常敏感SO下標籤

https://stackoverflow.com/questions/tagged/identityserver4

或下的問題GitHub的網站標記爲問題

https://github.com/IdentityServer/IdentityServer4/issues

隨着身份的服務器,再一次,我必須回到基礎,並通過輔導工作,以瞭解其將工作中的理解我的項目。

https://identityserver4.readthedocs.io/en/release/intro/big_picture.html

至於布洛克很快指出了我在另一篇文章,asp.net EF身份是用戶存儲和良好與資源所有者憑證的工作流程使用。

+0

我將使用Identity Server 4繼續這個動議 - 我們使用這個有多個域的一個身份驗證提供程序 –

0
+0

鏈接一個解決方案是受歡迎的,但請確保你的答案沒有它是有用的:[添加鏈接的上下文](// meta.stackexchange.com/a/8259),以便你的同行用戶將有一些想法是什麼,爲什麼它在那裏,然後引用您鏈接的頁面中最相關的部分,以防目標頁面不可用。 [僅僅是一個鏈接的答案可能會被刪除。](// stackoverflow.com/help/deleted-answers) – g00glen00b

相關問題