0

:웹 API에서 Owin 토큰 인증과 함께 Windows 인증을 사용하려면 어떻게해야합니까? 나는 웹 API를 응용 프로그램에서 일하고 나는 다음과 같은 인증 된 사용자를 확인하는 요구 사항이

1) Windows 인증 Windows에서 인증되지 않은 경우

2)를 사용하여 사용자를 인증합니다. Owin 액세스 토큰을 사용하여 사용자를 인증하려고 시도합니다.

내 코드가 작동하지만 Windows 인증을 사용하도록 설정하면 다음과 같다 : 다음

public static IAppBuilder EnableWindowsAuthentication(this IAppBuilder app) 
    { 
     if (!app.Properties.TryGetValue("System.Net.HttpListener", out var val)) 
      return app; 

     if (val is HttpListener listener) 
     { 
      listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication; 
     } 
     return app; 
    } 

Startup.cs 내부 :

public void Configuration(IAppBuilder app) 
    { 
     OnAppDisposing(app); 
     ConfigureOAuth(app); 

     var config = new HttpConfiguration(); 
     var webApiConfiguration = WebApiConfig.Register(config); 

     app.UseCors(CorsOptions.AllowAll); 
     app.EnableWindowsAuthentication(); 
     //here some owin middlewares 

     app.UseWebApi(webApiConfiguration); 

    } 
private void ConfigureOAuth(IAppBuilder app) 
    { 
     OAuthBearerOptions = new OAuthBearerAuthenticationOptions(); 

     OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() 
     { 
      AllowInsecureHttp = true, 
      TokenEndpointPath = new PathString("/api/token"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60), 
      Provider = new SimpleAuthorizationServerProvider() 
     }; 

     // Token Generation 

     app.UseOAuthBearerAuthentication(OAuthBearerOptions); 
     app.UseOAuthAuthorizationServer(OAuthServerOptions); 
     } 

내가 얻을 무기명 토큰을 사용하여 인증 된 엔드 포인트를 호출하려고하면 401 UnAuthorized.

제 질문은이 시나리오를 해결하고 두 인증 방법을 함께 사용하는 방법입니다.

답변

0

나는 이런 식으로 해결했다 :

public string FindUser(string activeDirectoryPath ,string userName, string password) 
    { 
     try 
     { 

       using (var searcher = new DirectorySearcher(new DirectoryEntry(activeDirectoryPath, userName, password))) 
       { 
        searcher.Filter = string.Format("(&(objectClass=user)(name={0}))", userName); 
        searcher.PropertiesToLoad.Add("name");// username 
        var activeDirectoryStaff = searcher.FindOne(); 
        if (activeDirectoryStaff != null) 
        { 
         return (string)activeDirectoryStaff.Properties["name"][0]; 
        } 
        else 
         return null; 
       } 
      } 

     } 
     catch (Exception ex) 
     { 
      this.Log().Error(ex, ex.Message); 
      return null; 
     } 
     return null; 
    } 

위의 방법은 경우 : GrantResourceOwnerCredentials 내가 Active Directory에 내부 사용자를 확인하기 위해 다음 코드를 사용합니다 방법 내부

SimpleAuthorizationServerProvider 내부 클래스를 null을 반환하면 401 UnAuthorized가 반환됩니다.