在没有身份的情况下为.net core创建besopoke第三方授权



我正在尝试建立.net核心站点,它是用另一种语言编写的更大的遗留系统的一部分。我不想在这个阶段重写主系统的登录/授权,所以我需要用户能够通过主系统登录,并将他们登录的凭据和授权角色传递到新的.net系统中。

我不想在新系统中维护一个单独的用户数据库,所以将有效地使用旧系统作为第三方身份验证服务。我正在努力寻找实现这一目标的最佳方式的例子。我想象我可以在两个系统之间编写一个OAuth进程。

我基本上希望新系统与旧系统核对是否有人以访问权限登录,如果有,则让他们继续登录,如果没有,则将他们弹回旧系统登录。

在新网站上使用没有身份的Cookie身份验证,并以某种方式将重定向/OAuth过程写入到遗留系统,这是最好的做法吗?有人知道如何做到这一点的例子或指导方针吗?我所能找到的只是链接到预设的第三方提供商列表(即谷歌、脸书等(的例子。

我可以在旧系统上编写OAuth部分,只是不确定如何在.net核心站点端实现它。和往常一样,任何指导意见都非常有价值。

据我所知,asp.net核心包含可以与OAuth一起工作的OAuth 2.0身份验证中间件。

关于如何使用它的更多细节,你可以参考下面的例子:

注意:您似乎使用了owin身份验证服务器,您应该自己替换参数。

services.AddAuthentication(options =>
{
// If an authentication cookie is present, use it to get authentication information
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// If authentication is required, and no cookie is present, use Yourauth (configured below) to sign in
options.DefaultChallengeScheme = "Yourauth";
})
.AddCookie() // cookie authentication middleware first
.AddOAuth("Yourauth", options =>
{
// Oauth authentication middleware is second
var Domain = Configuration.GetValue<string>("yourdomain");
// When a user needs to sign in, they will be redirected to the authorize endpoint
options.AuthorizationEndpoint = $"{Domain}/oauth2/default/v1/authorize";
// if your OAuth server is OpenID compliant, so request the standard openid
// scopes when redirecting to the authorization endpoint
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
// After the user signs in, an authorization code will be sent to a callback
// in this app. The OAuth middleware will intercept it
options.CallbackPath = new PathString("/authorization-code/callback");
// The OAuth middleware will send the ClientId, ClientSecret, and the
// authorization code to the token endpoint, and get an access token in return
options.ClientId = Configuration.GetValue<string>("ClientId");
options.ClientSecret = Configuration.GetValue<string>("ClientSecret");
options.TokenEndpoint = $"{Domain}/oauth2/default/v1/token";
// Below we call the userinfo endpoint to get information about the user
options.UserInformationEndpoint = $"{Domain}/oauth2/default/v1/userinfo";
// Describe how to map the user info we receive to user claims
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "sub");
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "given_name");
options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
// Get user info from the userinfo endpoint and use it to populate user claims
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
context.RunClaimActions(user);
}
};
});

最新更新