2

I am new to learn filters in mvc. I create a Authorization filter in my project.

Accountcontroller

 public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Logins()
        {
            string username = Request["username"];
            string password = Request["password"];
            Session.Add("username", username);
            Session.Add("password", password);

            return Redirect("/Home");
        }

    }

    public class CustomAuthorizationAttribute : FilterAttribute, IAuthorizationFilter
    {  
        void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
        {
            try
            {
                string username = HttpContext.Current.Session["username"].ToString();
                string password = HttpContext.Current.Session["password"].ToString();

                if (username == password)
                {

                    HttpContext.Current.Response.Redirect("/Home");

                }
                else
                {

                    HttpContext.Current.Response.Redirect("/Account/login");
                }
            }
            catch
            {
                HttpContext.Current.Response.Redirect("/Account/login");
            }
        }

    }

Homecontroller

public class HomeController : Controller
    {
        //
        // GET: /Home/
        [CustomAuthorization]
        public ActionResult Index()
        {

            return View();
        }

    }

But now i am checking same string as username and password when i run this project if the username and password is correct home page is reloading again and again.

4
  • if username and password are correct then it will load the index (default action) action in homecontroller because you write HttpContext.Current.Response.Redirect("/Home"); Commented Jan 6, 2016 at 8:59
  • But it does not go to home index. Home page in redirect loop. Commented Jan 6, 2016 at 9:01
  • Is this intentional? 'if (username == password)' Commented Jan 6, 2016 at 9:27
  • I am just checking in future i will change this code. Commented Jan 6, 2016 at 9:49

1 Answer 1

1

Inherit from Authorization attribute and override default behavior Simple implementation would be like this

public class OptionalAuthorizeAttribute : AuthorizeAttribute
{

public OptionalAuthorizeAttribute()
{

}

protected override bool AuthorizeCore(HttpContext httpContext){
            string username = HttpContext.Current.Session["username"].ToString();
            string password = HttpContext.Current.Session["password"].ToString();

            if (username == password)
            {
                return true;
            }
                return base.AuthorizeCore(httpContext);
    }
}

And then you could override behavior of AuthorizeAttribute.HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext)

Side note: I wrote this answer from mobile phone, so please double check for syntax errors when pasting to visual studio

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.