小编Kub*_*uba的帖子

Asp.Net MVC 6 Cookie身份验证 - 授权失败

我正在尝试使用Cookie中间件身份验证创建asp.net核心mvc 6应用程序.我的代码编译没有错误,但即使成功登录后我也不是授权用户

这是我的startup.cs配置

        app.UseCookieAuthentication(options =>
        {
            options.AuthenticationScheme = "CookieAuth";
            options.LoginPath = new PathString("/Account/Login/");
            options.AccessDeniedPath = new PathString("/Account/Login/");
            options.AutomaticAuthenticate = true;
            options.AutomaticChallenge = true;

        });
Run Code Online (Sandbox Code Playgroud)

还在我的控制器中登录操作:

   public async Task<IActionResult> Login(LoginViewModel model)
    {

        User foundUser = _userManager.findUser(model.UserName, model.Password);


        if (foundUser != null)
        {
            List<Claim> userClaims = new List<Claim>
            {
                new Claim("userId", Convert.ToString(foundUser.UserID)),
                new Claim(ClaimTypes.Name, foundUser.UserName),
                new Claim(ClaimTypes.Role, Convert.ToString(foundUser.RoleID))
            };

            ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
            await HttpContext.Authentication.SignInAsync("CookieAuth", principal);


            return RedirectToAction("Index", "Dashboard");
        }
        return View();
    }
Run Code Online (Sandbox Code Playgroud)

最后是Dashboard/Index动作

[Authorize]
public IActionResult Index() …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-core

15
推荐指数
1
解决办法
5522
查看次数

Entity Framework Core - 更新相关集合

我正在尝试更新 ProjectModel 中的 ProjectEmployees 集合。我想删除所有旧值并设置新值。

我的模型:

public class Project
{
    ... 
    public ICollection<ProjectEmployee> ProjectEmployees { get; set; }
}

public class ProjectEmployee
{
    public int ProjectId { get; set; }
    public virtual Project Project { get; set; }
    public int UserId { get; set; }
    public virtual Employee Employee { get; set; }
}

public class Employee
{
    public int UserId { get; set; }
    public User User { get; set; }
    ...
}
public class ProjectGroupModel //ViewModel
{
    public …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core asp.net-core

6
推荐指数
1
解决办法
8867
查看次数

子实体的继承和导航属性

我在使用继承(TPH - 目前在 EF Core 中唯一可用)时遇到导航属性问题。

我的层次结构模型:

public class Proposal
{
    [Key]
    public int ProposalId { get; set; }

    [Required, Column(TypeName = "text")]
    public string Substantiation { get; set; }

    [Required]
    public int CreatorId { get; set; }

    [ForeignKey("CreatorId")]
    public Employee Creator { get; set; }

}

public class ProposalLeave : Proposal
{
    [Required]
    public DateTime LeaveStart { get; set; }

    [Required]
    public DateTime LeaveEnd { get; set; }
}

public class ProposalCustom : Proposal
{
    [Required, StringLength(255)]
    public string …
Run Code Online (Sandbox Code Playgroud)

c# ef-database-first entity-framework-core asp.net-core

6
推荐指数
1
解决办法
2754
查看次数

Angular 2和Asp.Net 5 mvc 6 - 身份验证实践

我正在使用angular 2和asp.net 5 mvc 6创建我的第一个spa应用程序.它将是使用基于cookie的身份验证的简单应用程序.我知道我应该使用令牌,但我不需要将身份验证传递给其他应用程序,因此cookie应该足够了.我还决定使用基于权限的授权.Db架构看起来像:

User ---> Role <----> Permission
Run Code Online (Sandbox Code Playgroud)

当用户未经过身份验证时,我的api返回代码401;当他经过身份验证但是他没有访问数据的权限时,我返回403

我已经实现了这个部分: schema

但现在我不知道如何处理客户端中的用户凭据和权限,我有一些问题.我只需要一些指导.

  1. 如何在设置会话后在身份验证中将用户数据存储在spa中?有很多选择.我每次需要用户数据时都可以调用api,例如用户名,电子邮件,或者我可以获取一次并存储在加密的cookie或localStorage中.

  2. 与用户权限相同.我想管理模板中的按钮显示或菜单中的链接等,因为当前用户权限使用*ngIf等指令.我还可以在localStorage中存储用户权限,或者每次路由更改时调用api以获取json:

    {  
        showEditButton: true,
        showHeader: false 
    }
    
    Run Code Online (Sandbox Code Playgroud)

authentication asp.net-mvc session-cookies angular

2
推荐指数
1
解决办法
6633
查看次数