0

Web APIでOwinトークン認証と組み合わせてWindows認証を有効にするにはどうすればよいですか?私は、Web 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が返されます。

関連する問題