ASP MVC RouteConfig与身份验证返回erorr存储库未找到

时间:2014-05-12 04:48:07

标签: asp.net-mvc-routing asp.net-authorization

我尝试使用我的git客户端在我的asp中进行授权,因此我的git客户端将被请求来自我的服务器的授权。当我试图向我的git客户端发送请求时,它显示错误

repository http://localhost/git/user/try.git/info/ref not found

这是我的routeconfig

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        #region remoteURL
        routes.MapRoute(
            name: "RemoteURL",
            url: "git/{project}.git/{*verb}",
            defaults: new { controller = "Git", action = "Smart" }
            );

        routes.MapRoute(
            name: "Git",
            url: "git/{project}/{*verb}",
            defaults: new { controller = "Git", action = "Smart" }
        );
        #endregion
        #region Account;
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        #endregion;
    }

这是我的控制器使用属性:

public class GitController : Controller
{
    [SmartGit]
    public ActionResult Smart(string project, string service, string verb)
    {
        switch (verb)
        {
            case "info/refs":
                return InfoRefs(project, service);
            case "git-upload-pack":
                return ExecutePack(project, "git-upload-pack");
            case "git-receive-pack":
                return ExecutePack(project, "git-receive-pack");
            default:
                return RedirectToAction("Tree", "Repository", new { Name = project });
        }
    }

然后这是我的属性smartgit

public class SmartGitAttribute : SmartAuthorizeAttribute
{
    private const string AuthKey = "GitCodeGitAuthorize";
    private GitCodeContext db = new GitCodeContext();
    private string project;
    private string verb;
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);
        var right = false;
        var userfound = false;
        List<string> paramParsing = new List<string>();


        //url.Split("")
        //base.OnAuthorization(filterContext);
        var controller = filterContext.Controller as GitController;
        if (controller == null)
            return;

        // git.exe not accept cookies as well as no session available
        var auth = controller.HttpContext.Request.Headers["Authorization"];

        if (!String.IsNullOrEmpty(auth))
        {
            var bytes = Convert.FromBase64String(auth.Substring(6));
            var certificate = Encoding.ASCII.GetString(bytes);
            var index = certificate.IndexOf(':');
            var password = certificate.Substring(index + 1);
            var username = certificate.Substring(0, index);

            //var user = controller.MembershipService.Login(username, password);
            if (WebSecurity.Login(username, password))
            {
                WebSecurity.Login(username, password);
                userfound = true;
            }
        }

        var projectField = controller.ValueProvider.GetValue("project");
        var serviceField = controller.ValueProvider.GetValue("service");
        var verbField = controller.ValueProvider.GetValue("service");
        //filterContext.Controller.ValueProvider
        var project = projectField == null ? null : projectField.AttemptedValue;
        var service = serviceField == null ? null : serviceField.AttemptedValue;
        var verb = verbField == null ? null : serviceField.AttemptedValue;

        if (string.IsNullOrEmpty(service) && userfound) // redirect to git browser
        {
            right = true;
        }
        else if (string.Equals(service, "git-receive-pack", StringComparison.OrdinalIgnoreCase) && userfound) // git push
        {
            //right = controller.RepositoryService.CanWriteRepository(project, username);
            right = true;
        }
        else if (string.Equals(service, "git-upload-pack", StringComparison.OrdinalIgnoreCase) && userfound ) // git fetch
        {
            //right = controller.RepositoryService.CanReadRepository(project, username);
            right = true;
        }

        if (!userfound)
        {
            if (WebSecurity.CurrentUserName == "")
            {
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.AddHeader("WWW-Authenticate", "Basic realm=\"coba\"");
                filterContext.Result = new HttpUnauthorizedResult();
            }
            else
            {
                throw new UnauthorizedAccessException();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我发现自己的错误,也许我的回复没有足够的信息所以我决定在我的SmartGitAttribute中添加一些信息

filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusDescription = "Unauthorized";
filterContext.HttpContext.Response.AddHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"");
filterContext.HttpContext.Response.Write("401, please authenticate");
filterContext.HttpContext.Response.StatusCode = 401;
filterContext.Result = new EmptyResult();
filterContext.HttpContext.Response.End();

这是可以帮助您解决response authentication

的参考