【问题标题】:Identity Server and web api for user management用于用户管理的 Identity Server 和 web api
【发布时间】:2016-07-12 00:41:52
【问题描述】:

我在我的项目中使用 Identity Server3,我目前有一个网站和 api 受 Id 服务器保护,这工作正常但是因为我将用户存储在 Id Server 数据库中,我无法真正改变来自网站的任何用户数据,例如更改个人资料图片或任何声明价值。

为了解决这个问题,我正在考虑在 IdServer 之上创建一个 API,这个 API 将管理用户、更改密码、检索用户或更改与用户相关的任何内容,我想创建这个 API我的 IdServer 使用 Owin 映射的示例项目。

现在我的 idServer 像这样在 /identity 路由中

public class Startup
{
    public void Configuration(IAppBuilder app)
    {

        Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.Trace().CreateLogger();            

        app.Map("/identity", idserverApp =>
         {
             var efConfig = new EntityFrameworkServiceOptions
             {
                 ConnectionString = "IdSvr3Config"
             };

             var options = new IdentityServerOptions
             {
                 SiteName = "Identity server",
                 IssuerUri = ConfigurationManager.AppSettings["idserver:stsIssuerUri"],
                 PublicOrigin = ConfigurationManager.AppSettings["idserver:stsOrigen"],
                 SigningCertificate = Certificate.Get(),
                 Factory = Factory.Configure(efConfig),
                 AuthenticationOptions = AuthOptions.Configure(app),
                 CspOptions = new CspOptions { Enabled = false },
                 EnableWelcomePage=false
             };


             new TokenCleanup(efConfig, 3600 * 6).Start();
             idserverApp.UseIdentityServer(options);
         });

        app.UseIdServerApi();

    }        
}

我的api“中间件”是这样的

**public static class IdServerApiExtensions
    {
        public static void UseIdServerApi(this IAppBuilder app, IdServerApiOptions options = null)
        {
            if (options == null)
                options = new IdServerApiOptions();            

            if (options.RequireAuthentication)
            {
                var dic = app.Properties;
                JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
                app.UseIdentityServerBearerTokenAuthentication(
                    new IdentityServerBearerTokenAuthenticationOptions
                    {
                        Authority = "https://localhost:44302/identity", //here is the problem, it says not found even though I already mapped idServer to /identity
                        RequiredScopes = new[]
                        {
                        "idserver-api"
                        },
                        ValidationMode=ValidationMode.ValidationEndpoint
                    });
            }
            var config = new HttpConfiguration();
            WebApiConfig.Register(config,options.RequireAuthentication);
            app.UseNinjectMiddleware(() => NinjectConfig.CreateKernel.Value);
            app.UseNinjectWebApi(config);
            app.Use<IdServerApiMiddleware>(options);
        }
    }**

我知道有可能我只是不知道在同一个项目上使用 api 是否是个好主意,而且我也不知道该怎么做。

  • 创建此 API 来管理用户是个好主意吗?如果不是,我可以使用什么?
  • 如何创建适当的设置来启动 /api 下的 API,同时使用 IdServer 保护此 api?

更新

添加 ValidationMode=ValidationMode.ValidationEndpoint,解决了我的问题,感谢 Scott Brady

谢谢

【问题讨论】:

    标签: c# asp.net-web-api oauth-2.0 owin identityserver3


    【解决方案1】:

    Identity Server 团队的一般建议是将任何管理页面或 API 作为单独的项目运行 (Most recent example)。最佳做法是只让您的身份服务器和身份管理应用程序访问您的身份数据库/存储。

    要管理您的用户,是的,您可以编写自己的 API。其他选项是将其包含到单个 MVC 网站或使用类似 Identity Manager 的内容。

    但是,您仍然可以使用相同的应用程序方法,使用 OWIN 映射。为了确保这一点,您可以使用IdentityServer3.AccessTokenValidation 包,使用以下代码:

    app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
    {
        Authority = ConfigurationManager.AppSettings["idserver:stsOrigen"],
        RequiredScopes = new[] { "adminApi" },
        ValidationMode = ValidationMode.ValidationEndpoint
    });
    

    【讨论】:

    • 嗨,斯科特,当您说另一个项目时,您是否还意味着在另一个 url 中?,我希望身份服务器和 api 在同一个域下但在不同的路径上,我添加了api 并且它在 /api 下正常工作,但是如果我使用 IdServer 打开身份验证,它就会停止工作,因为我的猜测是 Owin 尚未加载导致授权端点失败的 idserver,知道吗?
    • 检查我的问题,我添加了用于 api 设置的代码
    • 您可以保留相同的 url(使用不同的路径),只需将代码分开即可。如果这适用于您,您可以将项目(想想 VS 解决方案)部署到 IIS 中同一 url 上的不同路径。在 OWIN 路径中使用 AccessTokenValidation 中间件应该没问题,如果它在启动时失败,那么这听起来像是一个单独的问题/问题。我已经看到项目在与以前相同的应用程序中使用 Identity Server 实现。它确实有效。
    • 是的,我在另一个项目中添加了我的 api,但我无法解决使用身份验证时权限失败的问题
    • 谢谢,添加这个ValidationMode = ValidationMode.ValidationEndpoint 修复它,我在github上找不到ValidationMode参数的解释,你知道任何url
    猜你喜欢
    • 2018-02-02
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2011-09-07
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多