Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:08:07+00:00 2026-05-31T09:08:07+00:00

I am trying to make authorize by using web.config. In my user registration, it

  • 0

I am trying to make authorize by using web.config.
In my user registration, it is not using ASP.NET Configuration.
I am handling the login page with database.
I want to protect admin page as manual typing in address from other people.
I put this code in Web.config.

//Web.config
<location path="Product">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>

When admin log in website from homepage which has partial logon page,
It will get userName and admin whether is false or true through database.

[HttpPost]
    public ActionResult Index(Customer model)
    {
        if (ModelState.IsValid)
        {
            //define user whether admin or customer
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["rentalDB"].ToString());
            String find_admin_query = "SELECT admin FROM Customer WHERE userName = '" + model.userName + "' AND admin ='true'";
            SqlCommand cmd = new SqlCommand(find_admin_query, conn);
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            //it defines admin which is true or false
            model.admin = sdr.HasRows;
            conn.Close();

            //if admin is logged in
            if (model.admin == true) {
                if (DAL.UserIsVaild(model.userName, model.password))
                {
                    FormsAuthentication.SetAuthCookie(model.userName, true);
                    return RedirectToAction("Index", "Product");
                }
            }

            //if customer is logged in
            if (model.admin == false) { 
                if (DAL.UserIsVaild(model.userName, model.password))
                {
                    FormsAuthentication.SetAuthCookie(model.userName, true);                   
                    return RedirectToAction("Index", "Home");
                }
            }
                ModelState.AddModelError("", "The user name or password is incorrect.");
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

Then my question is, how can I define the user by web.config instead of “*”, like using model.userName or model.admin? Could you tell me how to define the users? thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:08:08+00:00Added an answer on May 31, 2026 at 9:08 am

    Firstly, you cannot use the authorization element in the web.config to protect paths like you can for ASP.NET WebForms. This is because the routes in MVC are not physical paths like in WebForms.

    Secondly, you may wish to roll your own MembershipProvider and RoleProvider, as it will integrate nicely with ASP.NET and MVC. it’s pretty trivial, and you can substitute your own DAL to fulfill the provider contracts.

    Here’s what your controllers might look like once you’ve implemented your own providers:

    public class AuthController : Controller
    {
        public ActionResult Index(Customer model)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.userName, model.password))
                {
                    if (Roles.IsUserInRole(model.userName, "admin")) return RedirectToAction("Index", "Product");
    
                    return RedirectToAction("Index", "Home");
                }
    
                ModelState.AddModelError("", "The user name or password is incorrect.");
            }
            // If we got this far, something failed, redisplay form
            return View(model); 
        }
    }
    
    [Authorize(Roles = "user")]
    public class HomeController : Controller
    {
    
        public ActionResult Index()
        {
            return View();
        }
    }
    
    [Authorize(Roles = "admin")]
    public class ProductController : Controller
    {
    
        public ActionResult Index()
        {
            return View();
        }
    }
    

    If you don’t want to make your own providers, there are two other options to get the same functionality as the [Authorization] decorations:

    1. Subscribe to the AuthenticateRequest event in your global.asax.cs, check to make sure the User.Identity.IsAuthenticated property is true (which it will be able to tell you from the forms auth ticket will have been processed for you at this point). If it is true, load your roles from your DAL and create a new membership object, adding in the roles you found from the DAL. Now you can use AuthorizeAttribute anywhere else.

    2. Create your own derivative AuthorizeAttribute that uses your DAL to get the user’s roles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a post on another user's wall using another user
I'm trying to make a web service secure. It's not for a bank or
I'm trying to make a login and logout script for a page but for
Trying to make a web service call to an HTTPS endpoint in my Silverlight
trying to make a page which will recursively call a function until a limit
i'm trying to use OutputCaching in my ASP.NET MVC website. Problem is, when i
I have a (hopefully) basic question on credit card processing, particularly using Authorize.Net. This
I am new MVC 3 user and I am trying to make admin through
I am trying to make a WP7 app that would Tweet short messages user
I'm trying to use httplib to send credit card information to authorize.net. When i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.