登录到dotnetcore中的Slack&Add to Slack,而没有Identity Framework错误:oauth状态丢失或无效

问题描述 投票:1回答:1

我正在尝试为我的slackbot创建一个非常简单的页面,以便用户可以登录和注册。但是,即使使用他们生成的“使用松弛登录”按钮,我也会收到错误消息“ oauth状态丢失或无效。”。使用“添加到松弛”也会发生相同的错误。

我基于https://dotnetthoughts.net/slack-authentication-with-aspnet-core/创建了我的代码。即使它已经过时,但这也是我可以在网上找到的唯一示例。我试图弄清楚我需要进行哪些更改才能使其与dotnetcore 3和Slack 2.0一起使用,但是我已经不知所措了。

在我的服务中,在调用AddMvc等之前,我具有以下条件:>

services.AddAuthentication(options =>
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.Cookie.Name = "MyAuthCookieName";
        options.Cookie.HttpOnly = true;
        options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        options.Cookie.MaxAge = TimeSpan.FromDays(7);
        options.ExpireTimeSpan = TimeSpan.FromDays(7);

        options.LoginPath = $"/login";
        options.LogoutPath = $"/logout";
        options.AccessDeniedPath = $"/AccessDenied";
        options.SlidingExpiration = true;
        options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
    })
    //.AddSlack(options =>
    //{
    //    options.ClientId = Configuration["Slack:ClientId"];
    //    options.ClientSecret = Configuration["Slack:ClientSecret"];
    //});
    .AddOAuth("Slack", options =>
    {
        options.ClientId = Configuration["Slack:ClientId"];
        options.ClientSecret = Configuration["Slack:ClientSecret"];
        options.CallbackPath = new PathString("/signin-slack");
        options.AuthorizationEndpoint = $"https://slack.com/oauth/authorize";
        options.TokenEndpoint = "https://slack.com/api/oauth.access";
        options.UserInformationEndpoint = "https://slack.com/api/users.identity?token=";
        options.Scope.Add("identity.basic");
        options.Events = new OAuthEvents()
        {
            OnCreatingTicket = async context =>
            {
                var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint + context.AccessToken);
                var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted);
                response.EnsureSuccessStatusCode();
                var userObject = JObject.Parse(await response.Content.ReadAsStringAsync());
                var user = userObject.SelectToken("user");
                var userId = user.Value<string>("id");

                if (!string.IsNullOrEmpty(userId))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId, ClaimValueTypes.String, context.Options.ClaimsIssuer));
                }

                var fullName = user.Value<string>("name");
                if (!string.IsNullOrEmpty(fullName))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.Name, fullName, ClaimValueTypes.String, context.Options.ClaimsIssuer));
                }
            }
        };
    });

我的配置方法看起来像

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.Map("/login", builder =>
{
    builder.Run(async context =>
    {
        await context.ChallengeAsync("Slack", properties: new AuthenticationProperties { RedirectUri = "/" });
    });
});

app.Map("/logout", builder =>
{
    builder.Run(async context =>
    {
        await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        context.Response.Redirect("/");
    });
});

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapRazorPages();
});

除了“ oauth状态在无效时丢失”之外,如果在我的应用程序中我直接进入/ login我没有收到错误,但由于User.Identity.IsAuthenticated为假,因此我似乎没有登录。

我真的很茫然,可以使用一些非常感谢的帮助!

谢谢!

我正在尝试为我的slackbot创建一个非常简单的页面,以便用户可以登录和注册。但是,即使使用他们生成的“使用松弛登录”按钮,我也会收到错误消息“ oauth状态为...

c# .net-core slack slack-api
1个回答
1
投票

您提到的同一篇文章下面有一个指向AspNet.Security.OAuth.Providers源存储库的链接。这似乎相当活跃,并且支持其他OAuth目标(包括Slack)的HEAPS。

© www.soinside.com 2019 - 2024. All rights reserved.