0

Я разрабатываю веб-api в .NET Framework. Ui находится в угловом. У меня несколько пользователей с разными ролями в системе. Какая простая и лучшая аутентификация для этого веб-авио?Простая аутентификация для aspnet web api core

Пытался сделать Identity server4, но мне не нужно перенаправление на один знак, поэтому мне не нужна эта сложная реализация. Все, что я хочу, - это защитить api, чтобы незарегистрированные/анонимные пользователи не могли получить к нему доступ.

Вещи, которые я пробовал до сих пор, являются «промежуточным ПО cookie без идентификатора aspnet». Работает немного, но реальная проблема заключается в том, что я не уверен, как вернуть cookie из api и передать его обратно с ui.

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie

Я посмотрел в Интернете, но большинство статей для MVC. Поскольку это веб-api, мне нужно передать что-то вызывающему, чтобы они могли передать его обратно в api, который будет аутентифицирован.

Надеюсь, что это имеет смысл.

высоко ценю вашу помощь

Благодарности

ответ

1

Существует очень хорошая статья here, которая идет в большие подробности о authenticaton и авторизации. В статье также описывается, как можно применить проверку подлинности на весь API, к контроллеру, к одному методу и т.д. Все код для учебника на CodePlex here и я копирую один для базовой аутентификации ниже:

using System.Threading; 
using System.Threading.Tasks; 
using System.Web.Http.Filters; 
using BasicAuthentication.Results; 

namespace BasicAuthentication.Filters 
{ 
    public abstract class BasicAuthenticationAttribute : Attribute, IAuthenticationFilter 
    { 
     public string Realm { get; set; } 

     public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken) 
     { 
      HttpRequestMessage request = context.Request; 
      AuthenticationHeaderValue authorization = request.Headers.Authorization; 

      if (authorization == null) 
      { 
       // No authentication was attempted (for this authentication method). 
       // Do not set either Principal (which would indicate success) or ErrorResult (indicating an error). 
       return; 
      } 

      if (authorization.Scheme != "Basic") 
      { 
       // No authentication was attempted (for this authentication method). 
       // Do not set either Principal (which would indicate success) or ErrorResult (indicating an error). 
       return; 
      } 

      if (String.IsNullOrEmpty(authorization.Parameter)) 
      { 
       // Authentication was attempted but failed. Set ErrorResult to indicate an error. 
       context.ErrorResult = new AuthenticationFailureResult("Missing credentials", request); 
       return; 
      } 

      Tuple<string, string> userNameAndPasword = ExtractUserNameAndPassword(authorization.Parameter); 

      if (userNameAndPasword == null) 
      { 
       // Authentication was attempted but failed. Set ErrorResult to indicate an error. 
       context.ErrorResult = new AuthenticationFailureResult("Invalid credentials", request); 
       return; 
      } 

      string userName = userNameAndPasword.Item1; 
      string password = userNameAndPasword.Item2; 

      IPrincipal principal = await AuthenticateAsync(userName, password, cancellationToken); 

      if (principal == null) 
      { 
       // Authentication was attempted but failed. Set ErrorResult to indicate an error. 
       context.ErrorResult = new AuthenticationFailureResult("Invalid username or password", request); 
      } 
      else 
      { 
       // Authentication was attempted and succeeded. Set Principal to the authenticated user. 
       context.Principal = principal; 
      } 
     } 

     protected abstract Task<IPrincipal> AuthenticateAsync(string userName, string password, 
      CancellationToken cancellationToken); 

     private static Tuple<string, string> ExtractUserNameAndPassword(string authorizationParameter) 
     { 
      byte[] credentialBytes; 

      try 
      { 
       credentialBytes = Convert.FromBase64String(authorizationParameter); 
      } 
      catch (FormatException) 
      { 
       return null; 
      } 

      // The currently approved HTTP 1.1 specification says characters here are ISO-8859-1. 
      // However, the current draft updated specification for HTTP 1.1 indicates this encoding is infrequently 
      // used in practice and defines behavior only for ASCII. 
      Encoding encoding = Encoding.ASCII; 
      // Make a writable copy of the encoding to enable setting a decoder fallback. 
      encoding = (Encoding)encoding.Clone(); 
      // Fail on invalid bytes rather than silently replacing and continuing. 
      encoding.DecoderFallback = DecoderFallback.ExceptionFallback; 
      string decodedCredentials; 

      try 
      { 
       decodedCredentials = encoding.GetString(credentialBytes); 
      } 
      catch (DecoderFallbackException) 
      { 
       return null; 
      } 

      if (String.IsNullOrEmpty(decodedCredentials)) 
      { 
       return null; 
      } 

      int colonIndex = decodedCredentials.IndexOf(':'); 

      if (colonIndex == -1) 
      { 
       return null; 
      } 

      string userName = decodedCredentials.Substring(0, colonIndex); 
      string password = decodedCredentials.Substring(colonIndex + 1); 
      return new Tuple<string, string>(userName, password); 
     } 

     public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken) 
     { 
      Challenge(context); 
      return Task.FromResult(0); 
     } 

     private void Challenge(HttpAuthenticationChallengeContext context) 
     { 
      string parameter; 

      if (String.IsNullOrEmpty(Realm)) 
      { 
       parameter = null; 
      } 
      else 
      { 
       // A correct implementation should verify that Realm does not contain a quote character unless properly 
       // escaped (precededed by a backslash that is not itself escaped). 
       parameter = "realm=\"" + Realm + "\""; 
      } 

      context.ChallengeWith("Basic", parameter); 
     } 

     public virtual bool AllowMultiple 
     { 
      get { return false; } 
     } 
    } 
+0

Спасибо за ваше предложение. Это также работает с базой ядра .net? –

+0

Я бы быстро попробовал, если бы я был вами, и посмотреть, не так ли. У меня нет ядра .net или я бы попробовал. – CodingYoshi

+0

Спасибо. Я использовал идентификатор Aspnet и работает до сих пор. –

Смежные вопросы