带有OAuth和基本身份验证的C#Web Api

带有OAuth和基本身份验证的C#Web Api,c#,authentication,asp.net-web-api,oauth,basic-authentication,C#,Authentication,Asp.net Web Api,Oauth,Basic Authentication,我有一个Asp.net web api,它是用OAuth配置的。现在我有了一个新的客户端,它不能使用Oauth,但希望使用相同端点url的基本身份验证 还没有找到任何方法来做到这一点。在此方面的任何帮助都将不胜感激。提前感谢公共类CustomAuthorizeAttribute:AuthorizeAttribute public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool Is

我有一个Asp.net web api,它是用OAuth配置的。现在我有了一个新的客户端,它不能使用Oauth,但希望使用相同端点url的基本身份验证

还没有找到任何方法来做到这一点。在此方面的任何帮助都将不胜感激。提前感谢

公共类CustomAuthorizeAttribute:AuthorizeAttribute
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        if ((Thread.CurrentPrincipal.Identity.Name?.Length ?? 0) <= 0)
        {
            AuthenticationHeaderValue auth = actionContext.Request.Headers.Authorization;
            if (string.Compare(auth.Scheme, "Basic", StringComparison.OrdinalIgnoreCase) == 0)
            {
                string credentials = UTF8Encoding.UTF8.GetString(Convert.FromBase64String(auth.Parameter));
                int separatorIndex = credentials.IndexOf(':');
                if (separatorIndex >= 0)
                {
                    string userName = credentials.Substring(0, separatorIndex);
                    string password = credentials.Substring(separatorIndex + 1);
                    var userManager = new MembershipUserManager();
                    var user = userManager.FindAsync(userName, password).Result;
                    if (user != null)
                        Thread.CurrentPrincipal = actionContext.ControllerContext.RequestContext.Principal = new GenericPrincipal(new GenericIdentity(userName, "Basic"), System.Web.Security.Roles.Provider.GetRolesForUser(userName));
                }
            }
        }
        return base.IsAuthorized(actionContext);
    }
}
{ 受保护的覆盖布尔已授权(HttpActionContext actionContext) { if((Thread.CurrentPrincipal.Identity.Name?.Length±0)=0) { 字符串用户名=凭据。子字符串(0,分隔索引); 字符串密码=凭据.Substring(separatorIndex+1); var userManager=new MembershipUserManager(); var user=userManager.FindAsync(用户名、密码).Result; 如果(用户!=null) Thread.CurrentPrincipal=actionContext.ControllerContext.RequestContext.Principal=new GenericPrincipal(new GenericEntity(用户名,“Basic”)、System.Web.Security.Roles.Provider.GetRolesForUser(用户名)); } } } 返回base.IsAuthorized(actionContext); } }
设置令牌身份验证(Oauth)后使用此代码,这对两者都适用:此属性应在任何地方使用(放弃授权)[包含角色],并将验证基本身份验证,而base.IsAuthorized(actionContext);将验证令牌方法(Oauth)

MembershipUserManager是我创建的一个自定义类,我想您应该使用Identity User Manager。公共类CustomAuthorizeAttribute:AuthorizeAttribute { 受保护的覆盖布尔已授权(HttpActionContext actionContext) { if((Thread.CurrentPrincipal.Identity.Name?.Length±0)=0) { 字符串用户名=凭据。子字符串(0,分隔索引); 字符串密码=凭据.Substring(separatorIndex+1); var userManager=new MembershipUserManager(); var user=userManager.FindAsync(用户名、密码).Result; 如果(用户!=null) Thread.CurrentPrincipal=actionContext.ControllerContext.RequestContext.Principal=new GenericPrincipal(new GenericEntity(用户名,“Basic”)、System.Web.Security.Roles.Provider.GetRolesForUser(用户名)); } } } 返回base.IsAuthorized(actionContext); } } 设置令牌身份验证(Oauth)后使用此代码,这对两者都适用:此属性应在任何地方使用(放弃授权)[包含角色],并将验证基本身份验证,而base.IsAuthorized(actionContext);将验证令牌方法(Oauth)


MembershipUserManager是我创建的一个自定义类,用于处理成员身份,我猜您会使用Identity User Manager。

可能重复感谢您的回复。这不是重复的。检查了链接。这里的问题是如何使用基于请求的OAuth/Basic身份验证。我认为这两种身份验证无法集成。我能想到的唯一可行的方法是让一个需要基本身份验证的网页,然后它可以获得一个承载令牌,并使用承载令牌与主应用程序进行身份验证。@修改任何可能有助于复制感谢回复的示例代码。这不是重复的。检查了链接。这里的问题是如何使用基于请求的OAuth/Basic身份验证。我认为这两种身份验证无法集成。我能想到的唯一可行的方法是让一个需要基本身份验证的网页,然后它可以获得一个承载令牌,并使用该承载令牌与主应用程序进行身份验证。@MoD任何有帮助的示例代码@Riste Golaboski。我会在这里尝试并更新。谢谢@Riste Golaboski。我将尝试这个并在这里更新。