第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

狀態(tài) cookie 無(wú)效。處理遠(yuǎn)程登錄時(shí)遇到錯(cuò)誤。ASP.NET Core MVC 外部社交登錄

狀態(tài) cookie 無(wú)效。處理遠(yuǎn)程登錄時(shí)遇到錯(cuò)誤。ASP.NET Core MVC 外部社交登錄

C#
炎炎設(shè)計(jì) 2023-07-22 18:19:38
在沒(méi)有 ASP.NET Core Identity 的情況下在 ASP.NET Core2.2 MVC Web 應(yīng)用程序中實(shí)現(xiàn)外部社交登錄。成功登錄 Google、Facebook、Twitter、LinkedIn 和 Microsoft 后,我在重定向回應(yīng)用程序時(shí)收到以下錯(cuò)誤。處理請(qǐng)求時(shí)發(fā)生未處理的異常。異常:無(wú)效的狀態(tài) cookie。地點(diǎn)未知異常:處理遠(yuǎn)程登錄時(shí)遇到錯(cuò)誤。Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler.HandleRequestAsync()以下是Startup.cs文件中的設(shè)置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";                                                   })
查看完整描述

1 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

externallogincallback您似乎想在登錄 Microsoft 帳戶后將請(qǐng)求重定向到。如果是這樣,則不應(yīng)microsoftOptions.CallbackPath設(shè)置externallogincallback. 通過(guò)此設(shè)置,來(lái)自 Microsoft 的所有請(qǐng)求都將由 OAuth 中間件而不是您自己的端點(diǎn)處理externallogincallback。

對(duì)于登錄后重定向頁(yè)面,您需要return Challenge(authenticationProperties, provider);通過(guò)設(shè)置authenticationProperties.authenticationProperties

請(qǐng)按照以下步驟操作:

  1. REDIRECT URI在 Azure 門戶中更改https://localhost:xxx/signin-microsoft

  2. 更改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?}");

        });

    }

}

家庭控制器


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 });

    }

}



查看完整回答
反對(duì) 回復(fù) 2023-07-22
  • 1 回答
  • 0 關(guān)注
  • 145 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)