目前,我已经创建了一个 Identity server 4 Web 应用程序,其中包含具有默认客户端 ID 和机密的外部登录提供程序。但我的目标是基于租户注册 Azure、Google、Facebook 等身份验证提供程序。
我使用了SaasKit多租户程序集,在这里我尝试了app.usepertenant()中间件。但是UseGoogleAuthentication()方法已经过时了,所以我无法使用这个 usepertenant 中间件实现多租户身份验证。
当前代码,
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddMicrosoftAccount(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
Run Code Online (Sandbox Code Playgroud)
预期的代码如下,
var authentication = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
if (tenant.hasMicrosoft)
{
authentication.AddMicrosoftAccount(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
}
if (tenant.hasGoogle)
{
authentication.AddGoogle(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
}
authentication.AddCookie( options =>
{
options.SlidingExpiration = true; …Run Code Online (Sandbox Code Playgroud) authentication multi-tenant asp.net-core identityserver4 saaskit
我在ASP.NET Core API项目中使用了OAuth cookie身份验证。所有API在此项目中均得到授权。成功登录后,可以从浏览器网址栏中访问所有API。当我尝试从其他域访问Ajax请求中的API时,总是返回Unauthorized。
在这里,如何从Ajax请求中识别用户是否已通过身份验证?
API domain = ".apidomain.com"
Client domain = ".clientdomain.com"
Run Code Online (Sandbox Code Playgroud)
API配置:
services.AddAuthentication("oAuthSecurityScheme")
.AddOAuth("login.microsoftonline.com",
options =>
{
....
....
}).AddCookie(
"oAuthSecurityScheme",
options =>
{
options.LogoutPath = new PathString("/logout");
options.LoginPath = new PathString("/api/v1/account/authorize");
options.ExpireTimeSpan = new TimeSpan(7, 0, 0, 0);
options.SlidingExpiration = true;
options.Cookie.Name = "CustomerOAuthCookie";
options.Events.OnRedirectToLogin = UnAuthorizedResponseAsync;
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.HttpOnly = false;
});
Run Code Online (Sandbox Code Playgroud)
客户申请:
$.ajax({
url: "https://localhost:44332/api/gettickets/1",
type: 'GET',
success: function (result)
{
alert(result);
console.log(result);
}
});
Run Code Online (Sandbox Code Playgroud)
注意:当我直接在浏览器中访问以下API时,它将返回正确的响应 https:// localhost:44332 / api / gettickets / …
我已经将Office 365 OAuth2登录身份验证用于ASP.NET Core API。直接从浏览器访问此API时,此方法工作正常。但是,当我从ajax请求/其他Web应用程序调用此API时,发生以下Cors策略错误。
Access to XMLHttpRequest at 'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?client_id=e0745314-9236-4fr2c-a2fg0-c19cjfsfrrrb6b&scope=api%3A%2F%2Fe0745314-9236-4fr2c-a2fg0-c19cjfsfrrrb6b%2Ftestapi&response_type=code&redirect_uri=https%3A%2F%2Flocalhost%3A44332%2Fsignin-oidc&state=CfDJ8Kp1w7Ui3OZMswaNrHvqNR2MF9qKa9w3PILEMBv8s_zxSa3sMK1pQLr2EuNexhz8eM6
iDdbO2ciuxInNPCtbO1KJ31O_zXvOA_sMXHbAhzzkXKN9QDmrHMUOiQQdjXjam4EqKlopDpcE2vUxcus
4WehJCUfCqdQZjMuzZS7ovrxslRX2ueRNFqpSDichJCf_iduXgFV1bNLRM8gK0TmjUrdkdYtyji7BNsNdPP
o9Fhad' (redirected from 'https://localhost:44332/api/login/account') **from origin 'null'** has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)
我试图在启动文件中使用以下方法解决此问题。但是,我仍然遇到这个问题。
ConfigureServices:
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
Run Code Online (Sandbox Code Playgroud)
配置:
app.UseCors("CorsPolicy");
**Client Application** : http://localhost:5000
**API** : http://localhost:44332
**Login provider** : Office 365
Run Code Online (Sandbox Code Playgroud)
请向我建议从Office 365获得授权后实现跨源和重定向到相应客户端应用程序(http:// localhost:5000)URL 的标准方法吗?
注意:从API重定向到Microsoft登录名时,“来源”变为“空”。
.net-core ×2
api ×1
asp.net-core ×1
cookies ×1
cross-domain ×1
multi-tenant ×1
oauth ×1
saaskit ×1