Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 404基本身份验证问题_C#_Asp.net Mvc_Asp.net Web Api_Asp.net Mvc Routing_Basic Authentication - Fatal编程技术网

C# 404基本身份验证问题

C# 404基本身份验证问题,c#,asp.net-mvc,asp.net-web-api,asp.net-mvc-routing,basic-authentication,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]);

我写这篇文章是为了寻求帮助,以了解我在下面的代码中可能出现的错误。我试图以试用用户身份登录,但客户端出现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" };
    }
更新代码:

        {
            ***IPrincipal principal = new GenericPrincipal(new GenericIdentity(user2.uUsername, user.uUsername, BasicAuthResponseHeaderValue), null);***
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;
        }
请任何人就解决方案或代码提供建议,我需要调查

非常感谢

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

所以,在创建resp之后,实际上返回它。或者,整理一下,比如

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

在调试中,您看到的是404,因为您将resp设置为NotFound。但是,您是返回resp还是稍后在代码中返回401?向我们展示您的返回语句,它是这里的关键。您好,我已经用返回语句更新了上面的代码。谢谢你的回复。嗨,谢谢你的回复。我之前删除了“var resp”代码,并使用了“返回未经授权的请求;”语句,这仍然会导致类似的问题,无法以试用用户身份登录,而只能通过调用“api/values”以完全用户身份登录。任何建议。对不起,你是说问题不是你想要404而不是401,而是真正的问题是没有试用用户可以在他们应该能够登录的地方登录?是的,这是正确的。很抱歉,我有点困惑,因为调试输出在客户端抛出了404错误,它抛出了401错误,所以我不确定要研究什么。谢谢你澄清这一点。我正在使用values api controller测试身份验证方法,但它无法区分用户角色。他们是我需要改变或研究的东西。如有可能,请告知。非常感谢。您是否已检查您尝试登录的用户的过期日期是否晚于今天?如果您对该行进行了注释,那么您是否可以使用试用版登录,即where s.sExpiryDate>=DateTime。现在&–显然将where放在下一行?我正在使用expiryDate测试试用用户,直到2015年,遗憾的是,我仍然遇到同样的问题。我真的很感谢你的帮助和时间。我试着删除where-date子句,并尝试过几次修改,但遗憾的是没有成功。这是我的原始sql试用查询-从[dbo].[UserDetails]u加入[dbo].[Subscriptions]s on u.uID=s.sUID--其中s.[sExpiryDate]>=getdate,其中s.sPID='163'。
        {
            ***IPrincipal principal = new GenericPrincipal(new GenericIdentity(user2.uUsername, user.uUsername, BasicAuthResponseHeaderValue), null);***
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;
        }
if (user == null || user2  == null)
{
    return new HttpResponseMessage(HttpStatusCode.NotFound)
    {
        Content = new StringContent(string.Format("access denied")),
    };
}
//...