404基本身份验证问题

时间:2014-04-15 14:57:38

标签: c# asp.net-mvc asp.net-web-api asp.net-mvc-routing basic-authentication

我正在写信寻求帮助,以便在下面的代码中理解我可能出错的地方。我试图以试用用户身份登录,但它在客户端发出401错误。但是,我尝试调试我的存储库,它导致我在下面的代码中出现以下错误,404状态代码错误。

       ClaimRole user = repository.trial(credentials[0], credentials[1]);
       ClaimRole user2 = repository.full(credentials[0], credentials[1]);

            if (user == null || user2  == null)
        {
            var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(string.Format("access denied")),
            };

        }// this bracket is highlighted while debugging through which the local window shows 404 status code
         else
        {
           IPrincipal principal = new GenericPrincipal(new GenericIdentity(user.Username, BasicAuthResponseHeaderValue), new string[] { user.role });
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;
        }

        return base.SendAsync(request, cancellationToken);

    }

存储库代码:

public ClaimRole trial(string username, string password)
 {
    var query = (from s in db.subs
                join u in db.user on s.sUID equals u.uID
                where s.sExpiryDate >= DateTime.Now &&
                u.uUsername == username &&
                u.uPassword == password
                select u).FirstOrDefault();

    if (query != null)
    {
        // Build a user and add the appropriate Trial role
        return new ClaimRole { Username = query.uUsername, Password = query.uPassword, role = "Trial" };
    }
    else
    {
        // No user was found
        return null;
    }
}

public ClaimRole full(string username, string password)
{
    var query = (from s in db.subs
                join u in db.user on s.sUID equals u.uID
                where s.sPID.Value == 163 &&
                u.uUsername == username &&
                u.uPassword == password
                select u).FirstOrDefault();

    if (query != null)
    {
        // Build a user and add the appropriate Trial role
        return new ClaimRole { Username = query.uUsername, Password = query.uPassword, role = "full" };
    }
    else
    {
        // No user was found
        return null;
    }
}

404问题:

 resp --    {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StringContent, Headers:

值API控制器:

 // [Authorize(Roles="full")]
   [Authorize]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

UserDeTail类:

    public UserDetail()
    {
        this.Subscription = new HashSet<Subscription>();
        this.Trial = new HashSet<Trial_Try>();
    }

    public int uID { get; set; }
    public string uUsername { get; set; }
    public string uPassword { get; set; }
    public string uOldPassword { get; set; }

    public virtual ICollection<Subscription> Subscription { get; set; }
    public virtual ICollection<Trial_Try> Trial { get; set; }
}

更新了存储库查询(没有角色) - 问题仍然保持不变:

public UserDetail full(string username, string password)
    {
        var query = from s in db.Subscriptions
                    join u in db.UserDetails on s.sUID equals u.uID
                    where s.sPID.Value == 163 &&
                    u.uUsername == username &&
                    u.uPassword == password
                    select u;

        return query.FirstOrDefault();
    }

    public UserDetail trial(string username, string password)
    {
        var query = from s in db.Subscriptions
                    join u in db.UserDetails on s.sUID equals u.uID
                    where s.sExpiryDate >= DateTime.Now &&
                    u.uUsername == username &&
                    u.uPassword == password
                    select u;

        return query.FirstOrDefault();
    }

更新代码:

        {
            ***IPrincipal principal = new GenericPrincipal(new GenericIdentity(user2.uUsername, user.uUsername, BasicAuthResponseHeaderValue), null);***
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;
        }

有人可以就解决方案或代码提出建议,我需要深入研究。

非常感谢。

1 个答案:

答案 0 :(得分:1)

您没有在任何地方使用resp。您将其设置为404但从未返回或使用它做任何事情。因此,它将永远不会被返回,您的代码将继续并返回401。

因此,在创建resp后实际返回它。或者,整理像......一样的东西。

if (user == null || user2  == null)
{
    return new HttpResponseMessage(HttpStatusCode.NotFound)
    {
        Content = new StringContent(string.Format("access denied")),
    };
}
//...