Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IdentityServer4 Asp.Net核心标识_C#_Asp.net Core_Identityserver4_Asp.net Core Identity_Asp.net Core Mvc 2.1 - Fatal编程技术网

C# IdentityServer4 Asp.Net核心标识

C# IdentityServer4 Asp.Net核心标识,c#,asp.net-core,identityserver4,asp.net-core-identity,asp.net-core-mvc-2.1,C#,Asp.net Core,Identityserver4,Asp.net Core Identity,Asp.net Core Mvc 2.1,我正在尝试使用Asp.Net核心标识创建基本标识Server4,如教程中所示 但是当我调用登录方法时: 返回质询(新的AuthenticationProperties{ 重定向URI=“/Home/Index” }“oidc”) 我得到一个404错误: http://localhost:5000/account/login?returnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dmvc%26redirect_uri%3Dhttps%25

我正在尝试使用Asp.Net核心标识创建基本标识Server4,如教程中所示

但是当我调用登录方法时:

返回质询(新的AuthenticationProperties{
重定向URI=“/Home/Index”
}“oidc”)

我得到一个404错误:

http://localhost:5000/account/login?returnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dmvc%26redirect_uri%3Dhttps%253A%252F%252Flocalhost%253A44391%252Fsignin-oidc%26响应\类型%3Dcode%2520id\令牌%26作用域%3Dopenid%2520profile%2520api1%26响应\模式%3Dform\ post%26即时消息%3D636682993147514721.ZDAMMI5ZTGTMWU3YI00ZJMZLTKYODMTZJBINW2.2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 1-I9t05aDHqR-5cxYtr0PVVM6PwBKy-1olSFH8uIc8ku0UJn7PY0WA%26x客户端SKU%3DID\U网络标准1\U 4%26x客户端版本%3D5.2.0

我是否需要其他视图和控制器?我认为Asp.Net核心标识中的内容将被使用

我的IdentityServer4配置:

 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.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddDefaultUI()
            .AddEntityFrameworkStores<ApplicationDbContext>();

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

        // configure identity server with in-memory stores, keys, clients and scopes
        services.AddIdentityServer()
            .AddSigningCredential("CN=tst")
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddAspNetIdentity<IdentityUser>();
    }


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        //app.UseHttpsRedirection();
        //app.UseCookiePolicy();


        app.UseStaticFiles();
        app.UseIdentityServer();
        app.UseMvcWithDefaultRoute();
        //app.UseMvc(routes =>
        //         {
        //             routes.MapRoute(
        //                 name: "default",
        //                 template: "{controller=Home}/{action=Index}/{id?}");
        //         });
    }
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
services.AddDefaultIdentity()
.AddDefaultUI()
.AddEntityFrameworkStores();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//使用内存存储、密钥、客户端和作用域配置identity server
services.AddIdentityServer()
.AddSigningCredential(“CN=tst”)
.AddInMemoryStedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.addAsNetIdentity();
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
app.UseHsts();
}
//app.UseHttpsRedirection();
//app.UseCookiePolicy();
app.UseStaticFiles();
app.UseIdentityServer();
app.UseMvcWithDefaultRoute();
//app.UseMvc(路由=>
//         {
//routes.MapRoute(
//名称:“默认”,
//模板:“{controller=Home}/{action=Index}/{id?}”);
//         });
}
我的客户端配置:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication("oidc")
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", optins =>
            {                                       
                optins.SignInScheme = "Cookies";
                optins.Authority = "http://localhost:5000";
                optins.RequireHttpsMetadata = false;

                optins.ClientId = "mvc";
                optins.ClientSecret = "secret";
                optins.ResponseType = "code id_token";
                optins.GetClaimsFromUserInfoEndpoint = true;                    

                optins.Scope.Add("openid");
                optins.Scope.Add("profile");
                //optins.Scope.Add("email");
                optins.Scope.Add("api1");

                optins.ClaimActions.Add(new JsonKeyClaimAction("role", "role", "role"));
                optins.SaveTokens = true;

            });

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


 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();       
        app.UseAuthentication();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
public void配置服务(IServiceCollection服务)
{
服务。添加身份验证(“oidc”)
.AddCookie(“Cookies”)
.AddOpenIdConnect(“oidc”,选项=>
{                                       
optins.signnscheme=“Cookies”;
选择权=”http://localhost:5000";
optins.RequireHttpsMetadata=false;
optins.ClientId=“mvc”;
optins.ClientSecret=“secret”;
optins.ResponseType=“代码id\u令牌”;
optins.GetClaimsFromUserInfoEndpoint=true;
optins.Scope.Add(“openid”);
选择范围添加(“配置文件”);
//选择范围添加(“电子邮件”);
选择权范围添加(“api1”);
添加(新的JSonKeyClaimation(“角色”、“角色”、“角色”));
optins.SaveTokens=true;
});
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseCookiePolicy();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}

IdentityServer4控制台中没有错误。

随着ASP.NET Core Identity 2.1的引入,视图、控制器等不再添加到使用Visual Studio或dotnet CLI生成的项目中。取而代之的是,它们是通过一个网络提供的

作为此更改的一部分,旧式URL(如示例中所示)也发生了更改。它们现在以
/Identity
作为前缀,并通过位于名为
Identity
区域的ASP.NET Core Razor页面提供

IdentityServer4,正如我所说的,它已经不存在了(导致404)。要解决此问题,请在代码中配置
IdentityServerOptions
对象以使用新位置:

只有两个与身份相关的URL
services.AddIdentityServer(options =>
    {
        options.UserInteraction.LoginUrl = "/Identity/Account/Login";
        options.UserInteraction.LogoutUrl = "/Identity/Account/Logout";
    })
    .AddSigningCredential("CN=tst")
    // ...