【问题标题】:Authentication and Authorization in ASP.NET Web API with roles带有角色的 ASP.NET Web API 中的身份验证和授权
【发布时间】:2015-08-21 14:34:59
【问题描述】:

带有 asp.net 身份的网络 api 这是我的代码:

public class ApplicationUser : IdentityUser
{

    public string FullNamme { get; set; }
    public string ProfileType { get; set; }

    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

如果是客户或专业人士,我想根据public string ProfileType { get; set; } 管理授权... 类似的东西[Authorize(TypeProfile="xxx")] ... 如果不可能,我如何向我的代码添加简单角色,以及如何定义默认登录页面以在用户未登录时重定向。

希望你能帮帮我..

【问题讨论】:

    标签: c# asp.net asp.net-web-api webforms


    【解决方案1】:

    你试过这个http://bitoftech.net/2015/03/31/asp-net-web-api-claims-authorization-with-asp-net-identity-2-1/

    参见方法 2。创建自定义声明授权属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 2016-01-12
      相关资源
      最近更新 更多