关于asp.net:IdentityServer4 SPA登录

IdentityServer4 SPA Login

我想创建一个SPA,然后通过我的自定义登录屏幕登录(前端在React中


Config.cs上定义客户端时,将使用默认的CORS实现。定义客户端

时,您只需要将spa应用程序的origin添加到AllowedCorsOrigins

1
2
3
4
5
6
7
8
9
10
11
                new Client
                {
                    ...

                    AllowedCorsOrigins =
                        {
                           "http://localhost:5001"
                        },

                    ...
                },

注意http://localhost:5001是来源,http://localhost:5001/是url

编辑:

要在SPA应用程序上具有登录UI,还需要修改IdentityServer的Startup类以添加cors,这是详细信息:

  • 将此代码添加到ConfigureServices方法中:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    services.AddCors(options => {
                    options.AddPolicy("default", policy =>
                    {
                        policy.WithOrigins("http://localhost:5001")
                            .AllowAnyHeader()
                            .AllowAnyMethod()
                            .AllowCredentials();
                    });
                });

  • 并将此代码转换为Configure方法
  • 1
    app.UseCors("default");