身份验证响应被拒绝,因为缺少状态参数

时间:2019-06-29 14:32:16

标签: c# asp.net-web-api openid asp.net-core-2.1 steam-web-api

我愿意在asp.net core 2.1中使用Steam登录, 我使用AspNet.Security.OpenId.Steam nuget包进行连接 ,当调用sigin方法时,客户端页面重定向到Steam并使用Steam登录后调用回我的服务器,但未通过身份验证的请求并被拒绝...

1-in Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env,IConfiguration configuration,ApplicationDbContext applicationDbContext,ApplicationDbContextBase applicationDbContextBase)
{

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseHsts();
            }


            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();


            app.UseCors(option => option.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseHttpsRedirection();


            AppHttpContext.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>());

            applicationDbContext.MigrateToLastChange();

        }

2-在service.cs中

public static IServiceCollection SetupNegatechApi(this IServiceCollection services, IConfiguration configuration)
        {
            //TODO: add services here...

            services.AddMvc()

                .AddJsonOptions(options =>
                {
                    options.SerializerSettings.ContractResolver =
                        new CamelCasePropertyNamesContractResolver();
                    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                });

            //Assign User & Role Model And DbContext To Identity
            services.AddIdentity<ApplicationIdentityUser, ApplicationIdentityRole>().AddDefaultTokenProviders().AddEntityFrameworkStores<ApplicationDbContextBase>();


            //Get Auth Key & Convert To Byte;
            var AuthInfo = configuration.GetSection("Auth").Get<AppSettings>();
            var SSKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AuthInfo.SecurityKey));


            //Config Identity Password & JWT Config
            services.Configure<IdentityOptions>(options =>
            {
                options.Password.RequiredLength = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
                options.Password.RequireLowercase = false;
                options.Password.RequireDigit = false;

            })
            .AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(option =>
            {
                option.RequireHttpsMetadata = false;
                option.SaveToken = true;


                option.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = AuthInfo.Issuer,
                    ValidAudience = AuthInfo.Audienc,
                    IssuerSigningKey = SSKey,
                    ClockSkew = TimeSpan.Zero
                };
            })
            .AddCookie()
            .AddSteam(op =>
            {
                configuration.Bind(op);
                op.ClaimsIssuer = AuthInfo.Issuer;
                op.SaveTokens = true;
                op.CallbackPath = "/api/Steam/SteamCallBack";
                op.RequireHttpsMetadata = false;
            });

            services.Configure<IISOptions>(op => op.AutomaticAuthentication = false);


            //Register Configuration For Dependncy Injection
            services.AddSingleton<IConfiguration>(configuration);

            services.AddSingleton<IFileProvider>(new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/$gallery")));

            return services;
        }

三合一控制器

    [ApiController]
    [ApiExplorerSettings(GroupName = "public")]
    [Route("api/[controller]/[action]")]
    public class SteamController : BaseController
    {

        [HttpPost]
        public async Task<IActionResult> Signin()
        {
            var auth = new AuthenticationProperties { RedirectUri = "/api/Steam/SteamCallBack" };
            return Challenge(auth,"Steam" );
        }

        [HttpGet]
        public IActionResult SteamCallBack(string state,openid openid)
        {
            //breack point
            return Redirect("http://localhost:3000/profile?id=" + "test");
        }

    }


    public class openid
    {
        public string claimed_id { get; set; }
        public string identity { get; set; }
        public string return_to { get; set; }
        public string response_nonce { get; set; }
        public string assoc_handle { get; set; }
        public string signed { get; set; }
        public string sig { get; set; }
    }

4-in html文件

<form id="steam_form" action="https://localhost:44315/api/Steam/Signin" method="post">
   //Submit Login form to api server
  <button type="submit"> Login</button>
</form>

回拨http://s8.picofile.com/file/8365103326/Untitled.png后出现5个结果错误

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但是AddSteam选项在OpenID规则之上。 如果仔细看,您会发现Steams OpenId 只是名称和一些随机标准。 检查您的表单,并将端点更改为your.address/signin并发布表单:

<form id="steamAuth" action="https://localhost:44315/signin" method="post">
  <input type='hidden' name='Provider' value='Steam'>
  <input type = 'hidden' name='ReturnUrl' value='your.address/returnurl'></form>
  <button type="submit"> Login</button>
</form>

不确定,但是我认为.AddSteam()选项不包括在服务配置中添加的任何设置。

如果您选中repo of this library,则可以看到示例,此处描述了其他提供程序时,它只是 AddSteam()

services.AddAuthentication(options => { /* Authentication options */ })
            .AddSteam()
            .AddOpenId("StackExchange", "StackExchange", options =>
            {
                options.Authority = new Uri("https://openid.stackexchange.com/");
                options.CallbackPath = "/signin-stackexchange";
            });