gpt4 book ai didi

c# - 状态 cookie 无效。处理远程登录时遇到错误。 ASP.NET Core MVC 外部社交登录

转载 作者:行者123 更新时间:2023-12-04 15:42:41 24 4
gpt4 key购买 nike

在没有 ASP.NET Core Identity 的 ASP.NET Core2.2 MVC Web 应用程序中实现外部社交登录时。成功登录 Google、Facebook、Twitter、LinkedIn 和 Microsoft 后,我​​在重定向回应用程序时遇到以下错误。

An unhandled exception occurred while processing the request. Exception: Invalid state cookie. Unknown location

Exception: An error was encountered while handling the remote login. Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()



enter image description here

以下是 Startup.cs 文件中的设置

public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services
.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.IsEssential = true;
})
.AddGoogle(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.ClientId = Configuration["Authentication:Google:ClientId"];
options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
options.CallbackPath = "/externallogincallback";
})
.AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
facebookOptions.CallbackPath = "/externallogincallback";
})
.AddLinkedIn(linkedinOptions =>
{
linkedinOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
linkedinOptions.ClientId = Configuration["Authentication:LinkedIn:ClientId"];
linkedinOptions.ClientSecret = Configuration["Authentication:LinkedIn:ClientSecret"];
linkedinOptions.CallbackPath = "/externallogincallback";
})
.AddTwitter(twitterOptions =>
{
twitterOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
twitterOptions.ConsumerKey = Configuration["Authentication:Twitter:ConsumerKey"];
twitterOptions.ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"];
twitterOptions.CallbackPath = "/Home/externallogincallback";
}).AddMicrosoftAccount(microsoftOptions =>
{
microsoftOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ClientId"];
microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
microsoftOptions.CallbackPath = "/externallogincallback";
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

以下是 HomeController.cs 的详细信息(因为我没有使用 Identity,所以我需要专门定义重定向 url。)

      //Action to issue a challange to google login
public IActionResult LogInMicrosoft(string provider)
{
//provider = Microsot or Google or LinkedIn or Twitter or Facebook
provider = "Microsoft";
//Issue a challenge to external login middleware to trigger sign in process
//return new ChallengeResult(provider);

var authenticationProperties = new AuthenticationProperties
{
RedirectUri = Url.Action("externallogincallback")
};

return Challenge(authenticationProperties, provider);
//return new ChallengeResult(provider);
}

//Callback action to retrive signin user details
//public IActionResult externallogincallback(string returnUrl = null, string remoteError = null)\
public IActionResult externallogincallback()
{
//Here we can retrieve the claims
// read external identity from the temporary cookie
//var authenticateResult = HttpContext.GetOwinContext().Authentication.AuthenticateAsync("ExternalCookie");
var result = HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);

if (result.Result?.Succeeded != true)
{
throw new Exception("External authentication error");
}

// retrieve claims of the external user
var externalUser = result.Result.Principal;
if (externalUser == null)
{
throw new Exception("External authentication error");
}

// retrieve claims of the external user
var claims = externalUser.Claims.ToList();

// try to determine the unique id of the external user - the most common claim type for that are the sub claim and the NameIdentifier
// depending on the external provider, some other claim type might be used
//var userIdClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject);
var userIdClaim = claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
if (userIdClaim == null)
{
throw new Exception("Unknown userid");
}

var externalUserId = userIdClaim.Value;
var externalProvider = userIdClaim.Issuer;

// use externalProvider and externalUserId to find your user, or provision a new user

return RedirectToAction("Privacy", "Home");
}

最佳答案

您似乎想将请求重定向到 externallogincallback登录微软账户后。如果是这样,你不应该设置 microsoftOptions.CallbackPathexternallogincallback .使用此设置,来自 Microsoft 的所有请求都将由 OAuth 中间件处理,而不是您自己的端点 externallogincallback .

对于登录后重定向页面,您需要通过 return Challenge(authenticationProperties, provider);通过设置 authenticationProperties.authenticationProperties
请按照以下步骤操作:

  • 更改 REDIRECT URI在 Azure 门户中使用 https://localhost:xxx/signin-microsoft
  • 更改 Startup.cs
    public class Startup
    {
    public Startup(IConfiguration configuration)
    {
    Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
    services.Configure<CookiePolicyOptions>(options =>
    {
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
    });


    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    services.AddAuthentication(options =>
    {
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    //options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
    })
    .AddCookie(options =>
    {
    options.Cookie.IsEssential = true;
    //options.Cookie.SameSite = SameSiteMode.None;
    })
    .AddMicrosoftAccount(microsoftOptions =>
    {
    microsoftOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ClientId"];
    microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
    });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseAuthentication();
    app.UseMvc(routes =>
    {
    routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");
    });
    }
    }
  • 家庭 Controller
    public class HomeController : Controller
    {
    //Action to issue a challange to google login
    public IActionResult LogInMicrosoft(string provider)
    {
    //provider = Microsot or Google or LinkedIn or Twitter or Facebook
    provider = "Microsoft";
    var authenticationProperties = new AuthenticationProperties
    {
    RedirectUri = Url.Action("externallogincallback")
    };
    return Challenge(authenticationProperties, provider);
    }

    [Route("/[action]")]
    public async Task<IActionResult> externallogincallback()
    {
    var request = HttpContext.Request;
    //Here we can retrieve the claims
    // read external identity from the temporary cookie
    //var authenticateResult = HttpContext.GetOwinContext().Authentication.AuthenticateAsync("ExternalCookie");
    var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);

    if (result.Succeeded != true)
    {
    throw new Exception("External authentication error");
    }

    // retrieve claims of the external user
    var externalUser = result.Principal;
    if (externalUser == null)
    {
    throw new Exception("External authentication error");
    }

    // retrieve claims of the external user
    var claims = externalUser.Claims.ToList();

    // try to determine the unique id of the external user - the most common claim type for that are the sub claim and the NameIdentifier
    // depending on the external provider, some other claim type might be used
    //var userIdClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject);
    var userIdClaim = claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
    if (userIdClaim == null)
    {
    throw new Exception("Unknown userid");
    }

    var externalUserId = userIdClaim.Value;
    var externalProvider = userIdClaim.Issuer;

    // use externalProvider and externalUserId to find your user, or provision a new user

    return RedirectToAction("Privacy", "Home");
    }
    public IActionResult Index()
    {
    return View();
    }

    public IActionResult Privacy()
    {
    return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
    }
  • 关于c# - 状态 cookie 无效。处理远程登录时遇到错误。 ASP.NET Core MVC 外部社交登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240285/

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