gpt4 book ai didi

asp.net-core - 如何从/signin-oidc 重定向回我的 Controller /操作?

转载 作者:行者123 更新时间:2023-12-05 01:40:23 24 4
gpt4 key购买 nike

回调地址为https://localhost:44338/signin-oidc

假设我在 controller/action 中,用 [Authorize] 装饰

我如何从 https://localhost:44338/signin-oidc 重定向回我的 Controller /操作?

注意:我正在关注 wiki: Quickstart: Add sign-in with Microsoft to an ASP.NET Core web app

最佳答案

您可以将 url 存储在服务器端。例如,基于代码示例:

Quickstart: Add sign-in with Microsoft to an ASP.NET Core web app

修改您的 OIDC 配置,例如:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));

services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
options.Authority = options.Authority + "/v2.0/";
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = async n =>
{
//save url to state
n.ProtocolMessage.State = n.HttpContext.Request.Path.Value.ToString();
},

OnTokenValidated = ctx =>
{
var url = ctx.ProtocolMessage.GetParameter("state");
var claims = new List<Claim>
{
new Claim("myurl", url)
};
var appIdentity = new ClaimsIdentity(claims);

//add url to claims
ctx.Principal.AddIdentity(appIdentity);

return Task.CompletedTask;
},

OnTicketReceived = ctx =>
{
var url = ctx.Principal.FindFirst("myurl").Value;
ctx.ReturnUri = url;
return Task.CompletedTask;
}



};
// Per the code below, this application signs in users in any Work and School
// accounts and any Microsoft Personal Accounts.
// If you want to direct Azure AD to restrict the users that can sign-in, change
// the tenant value of the appsettings.json file in the following way:
// - only Work and School accounts => 'organizations'
// - only Microsoft Personal accounts => 'consumers'
// - Work and School and Personal accounts => 'common'

// If you want to restrict the users that can sign-in to only one tenant
// set the tenant value in the appsettings.json file to the tenant ID of this
// organization, and set ValidateIssuer below to true.

// If you want to restrict the users that can sign-in to several organizations
// Set the tenant value in the appsettings.json file to 'organizations', set
// ValidateIssuer, above to 'true', and add the issuers you want to accept to the
// options.TokenValidationParameters.ValidIssuers collection
options.TokenValidationParameters.ValidateIssuer = false;
});

关于asp.net-core - 如何从/signin-oidc 重定向回我的 Controller /操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56755406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com