【问题标题】:How to enable windows authentication in conjunction with Owin token authentication in web api?如何在 web api 中启用 windows 身份验证和 Owin 令牌身份验证?
【发布时间】:2018-06-03 21:39:27
【问题描述】:

我正在开发一个 web api 应用程序,我需要检查经过身份验证的用户,如下所示:

1) 使用 Windows 身份验证

对用户进行身份验证

2) 如果未在 Windows 中进行身份验证。我将尝试使用 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);
      }

如果我尝试使用 Bearer 令牌调用授权端点,我会得到 401 UnAuthorized

所以我的问题是:如何解决这种情况并让两种身份验证方法一起工作?

【问题讨论】:

    标签: c# asp.net-web-api owin windows-authentication access-token


    【解决方案1】:

    我是这样解决的:

    GrantResourceOwnerCredentials 方法中的 SimpleAuthorizationServerProvider 类中,我将使用以下代码检查 Active Directory 中的用户:

    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;
        }
    

    如果上述方法返回null,那么我将返回 401 UnAuthorized 。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-25
      • 2015-07-18
      • 2014-07-06
      • 1970-01-01
      • 2017-05-15
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多