1

I need to authenticate the incoming request before it start processing. When my client application requests to the server, I need to authenticate the request using basic authentication and need to send response to the client.

I've tried the following but failed,

public class OptionalAuthentication : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContextWrapper httpContext = new HttpContextWrapper(System.Web.HttpContext.Current);
        HttpRequestBase httpRequest = httpContext.Request;
        if ((httpContext.User == null ? true : !httpContext.User.Identity.IsAuthenticated))
        {
            var request = HttpContext.Current.Request;
            //request.Headers.FirstOrDefault(h => h.Key.Equals("Authorization"));
            var authHeader = request.Headers["Authorization"];
            if (authHeader != null)
            {
                var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);

                // RFC 2617 sec 1.2, "scheme" name is case-insensitive
                if (authHeaderVal.Scheme.Equals("basic",
                        StringComparison.OrdinalIgnoreCase) &&
                    authHeaderVal.Parameter != null)
                {
                    if (AuthenticateUser(authHeaderVal.Parameter))
                    {

                    }
                }
            }
        }
        else
        {
            //log.Trace("user is already authenticated: '{0}'", httpContext.User.Identity.Name);
        }
    }
}


[OptionalAuthentication]
    public ActionResult Index(string projectSlug, string repositoryName)
    {
        ActionResult emptyResult;
        if (Request.IsAuthenticated)
        {
            var cred = System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(Request.Headers["Authorization"].Substring(6))).Split(':');
        }
    }

can somebody tell me a way to achieve this.

Gopinath Perumal
  • 2,258
  • 3
  • 28
  • 41

1 Answers1

1

here give u demo which is define about authenticate of client

here is about controller code

   [HttpPost]
   public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var userInfo = new UserInfo
            {
                UserName = model.UserName,
                Password = model.Password,
                //AppType = "Web"
            };

            var service = new ATWMSService();
            if(service.ValidateUser(userInfo))
            {
                Session["UserId"] = service.GetUserId(userInfo.UserName);
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                return Redirect("~/");
            }
            ModelState.AddModelError("","The user name or password provided is incorrect.");
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }   
MikeSW
  • 16,140
  • 3
  • 39
  • 53