Identity Server 和 web api 用于用户管理
Identity Server and web api for user management
我正在为我的项目使用 Identity Server3,我目前有一个网站并且 api 受到 Id 服务器的保护,这工作正常但是因为我将用户存储在 Id 服务器数据库中我无法真正更改网站上的任何用户数据,例如更改个人资料图片或任何声明值。
为了解决这个问题,我正在考虑在 IdServer 之上创建一个 API,这个 API 将管理用户、更改密码、检索用户或更改与用户基本上,我想在我使用 Owin 映射的 IdServer 示例项目上创建这个 API。
现在我的 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"middleware"是这样的
**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
谢谢
Identity Server 团队的一般建议是 运行 任何管理页面或 API 作为一个单独的项目 (Most recent example)。最佳做法是只允许您的身份服务器和身份管理应用程序访问您的身份 database/store.
要管理您的用户,是的,您可以编写自己的 API。其他选项是将其包含在单个 MVC 网站或使用类似 Identity Manager.
的内容
不过,您仍然可以使用相同的应用程序方法,使用 OWIN 地图。为了确保这一点,您可以使用 IdentityServer3.AccessTokenValidation 包,使用如下代码:
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["idserver:stsOrigen"],
RequiredScopes = new[] { "adminApi" },
ValidationMode = ValidationMode.ValidationEndpoint
});
我正在为我的项目使用 Identity Server3,我目前有一个网站并且 api 受到 Id 服务器的保护,这工作正常但是因为我将用户存储在 Id 服务器数据库中我无法真正更改网站上的任何用户数据,例如更改个人资料图片或任何声明值。
为了解决这个问题,我正在考虑在 IdServer 之上创建一个 API,这个 API 将管理用户、更改密码、检索用户或更改与用户基本上,我想在我使用 Owin 映射的 IdServer 示例项目上创建这个 API。
现在我的 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"middleware"是这样的
**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
谢谢
Identity Server 团队的一般建议是 运行 任何管理页面或 API 作为一个单独的项目 (Most recent example)。最佳做法是只允许您的身份服务器和身份管理应用程序访问您的身份 database/store.
要管理您的用户,是的,您可以编写自己的 API。其他选项是将其包含在单个 MVC 网站或使用类似 Identity Manager.
的内容不过,您仍然可以使用相同的应用程序方法,使用 OWIN 地图。为了确保这一点,您可以使用 IdentityServer3.AccessTokenValidation 包,使用如下代码:
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["idserver:stsOrigen"],
RequiredScopes = new[] { "adminApi" },
ValidationMode = ValidationMode.ValidationEndpoint
});