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# ASP.NET Core 3.1:Web API标识登录_C#_Asp.net Core_Asp.net Identity_Asp.net Core Webapi - Fatal编程技术网

C# ASP.NET Core 3.1:Web API标识登录

C# ASP.NET Core 3.1:Web API标识登录,c#,asp.net-core,asp.net-identity,asp.net-core-webapi,C#,Asp.net Core,Asp.net Identity,Asp.net Core Webapi,我正在为我的Web API创建CookieAutentation登录 就我而言,我已经阅读并遵循了这本书,并且我做的每件事都是正确的 但当我在控制器中设置断点并检查HttpContext.User时,所有内容都始终为空,没有用户名,没有声明,什么都没有 我还需要什么才能让它工作?Web API与MVC应用程序是否需要其他步骤 Startup.cs: public void ConfigureServices(IServiceCollection services) { services.

我正在为我的Web API创建CookieAutentation登录

就我而言,我已经阅读并遵循了这本书,并且我做的每件事都是正确的

但当我在控制器中设置断点并检查
HttpContext.User
时,所有内容都始终为空,没有用户名,没有声明,什么都没有

我还需要什么才能让它工作?Web API与MVC应用程序是否需要其他步骤

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();

    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, act => {
                act.LoginPath = "/api/login";
                act.AccessDeniedPath = "/api/login";
                act.SlidingExpiration = true;
            });

    services.AddControllers();

    services.AddServices(); // <- Own app domain services 
    services.AddDataAccess(); // <- Own app domain data access
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseCors(
            options => options.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
        );

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();
    app.UseRouting();

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

    app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
}
ToClaimsIdentity
扩展方法:

public static ClaimsIdentity ToClaimsIdentity(this SecurityUser user, string authenticantionType, int auditUserID)
{
        var claims = new List<Claim>()
        {
            new Claim(ClaimTypes.NameIdentifier, user.UserID.ToString()),
            new Claim(ClaimTypes.Email, user.Email),
            new Claim(ClaimTypes.Name, user.FirstName),
            new Claim(SecurityUserClaimTypes.AuditUserID, auditUserID.ToString())
        };

        var identity = new ClaimsIdentity(claims, authenticantionType);
        return identity;
}
public static ClaimsIdentity to ClaimsIdentity(此SecurityUser用户,字符串authenticationtype,int auditUserID)
{
var索赔=新列表()
{
新声明(ClaimTypes.NameIdentifier,user.UserID.ToString()),
新索赔(ClaimTypes.Email、user.Email),
新索赔(ClaimTypes.Name、user.FirstName),
新声明(SecurityUserClaimTypes.AuditUserID、AuditUserID.ToString())
};
var identity=新的索赔实体(索赔、认证类型);
返回身份;
}
任何帮助都将不胜感激


编辑-这就是我要讲的内容谢谢你们的帮助

我终于意识到这是客户的事情,我做了三件事:

  • CORS是一个问题,在我的
    中。UseCors
    方法调用我的Api我允许的凭据:
    .AllowCredentials()
  • 我的客户端应用在使用Blazor时,我发现它告诉我需要设置http请求配置以包括凭据,因此在客户端应用启动中。cs:
    WebAssemblyHttpMessageHandlerOptions.DefaultCredentials=FetchCredentialsOption.Include
  • 我在本地使用Http而不是Https,Chrome抱怨SameSite,所以我在我的Api StartUp.cs中调用了
    AddAuthentication…AddCookie
    我添加了以下内容:
    options.Cookie.SameSite=SameSiteMode.Unspecified
我不完全理解同一件事。。。我还遇到了JSON Web令牌(JWT)

但我不感兴趣,只要它起作用

您是否已将
[Authorize]
添加到控制器?
[Authorize]
已检查
用户。已授权
-但根本没有用户-因此添加
[Authorize]
to controllers只会阻止我访问控制器并返回一个
404
我刚刚将上面的代码片段复制到一个新的Web Api项目模板中,生成了一个cookie,当我将cookie发送回HttpContext时,用户ClaimsPrincipal已正确填充了以前设置的声明。因此,我无法对这个问题进行反驳。我是否正确理解您正在生成一个cookie,然后尝试发送另一个包含cookie的请求?因为如果您签入的请求与登录的请求相同,那么它还不会被填充;仅当ASP.NET对收到的cookie运行身份验证时。我有一个客户端应用程序(Blazor)和我的web API,它们位于不同的端口上。首先,我请求
api/login
,然后(另一个控制器)
api/production
-这就是我检查
用户身份的地方。
-它根本没有创建Cookie-当我使用F12开发工具时,没有任何需要启用的跨源设置?因为它们都在不同的端口上?
public static ClaimsIdentity ToClaimsIdentity(this SecurityUser user, string authenticantionType, int auditUserID)
{
        var claims = new List<Claim>()
        {
            new Claim(ClaimTypes.NameIdentifier, user.UserID.ToString()),
            new Claim(ClaimTypes.Email, user.Email),
            new Claim(ClaimTypes.Name, user.FirstName),
            new Claim(SecurityUserClaimTypes.AuditUserID, auditUserID.ToString())
        };

        var identity = new ClaimsIdentity(claims, authenticantionType);
        return identity;
}