带有角色的ASP.NET Web API中的身份验证和授权

时间:2015-08-21 14:35:00

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

带有asp.net身份的net web 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; }管理auhtorization,如果它是客户或专业... 类似于[Authorize(TypeProfile="xxx")] ...... 如果不可能,我如何向我的代码添加简单角色,如果用户未登录,如何定义默认登录页面以进行重定向。

我希望你能帮帮我..

1 个答案:

答案 0 :(得分:0)

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

请参阅方法2.创建自定义声明授权属性