Click here to Skip to main content
15,795,793 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how i restrict the user who enter the site without login
Posted
Comments
#realJSOP 25-Mar-11 9:58am    
This is a repost. DO NOT DO THAT. I deleted your earlier question because there were no answers in it.

Amend your web.config file, pay attention to the 'Authorisation' section

http://www.developerfusion.com/article/5385/highperformance-net-application-development-architecture/5/[^]

XML
<authorization>
   <!-- General application authorization -->
   <allow verbs="POST" users="Jimmy" roles="Administrators" />
   <allow verbs="GET" users="Peter" roles="Debugger Users" />
   <!-- URL Authorization -->
   <allow users="domain1\user, domain2\group" />
   <!--Deny anonymous or All Users-->
   <deny users="? | *" />
 </authorization>


This is the section where you can allow \ deny users based on credentials, group membership etc
 
Share this answer
 
Comments
Dalek Dave 25-Mar-11 10:23am    
Good Answer
XML
<pre lang="xml">&lt;authentication mode=&quot;Forms&quot;&gt;
            &lt;forms defaultUrl=&quot;Secured/Default.aspx&quot; loginUrl=&quot;Login.aspx&quot;&gt;
      &lt;/forms&gt;
        &lt;/authentication&gt;</pre>

<pre lang="xml">&lt;system.web&gt;
            &lt;authorization&gt;
                &lt;deny users=&quot;?&quot;/&gt;
            &lt;/authorization&gt;
        &lt;/system.web&gt;</pre>
in the code behind
<pre lang="cs">protected void btn_Submit_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == &quot;UserName&quot;)
            {
                if (TextBox2.Text == &quot;Password&quot;)
                {
                   // Response.Redirect(&quot;~/Secured/Secured2.aspx&quot;);
                    FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
                }
                else
                {
                    Label1.Text = &quot;Enter Correct Password&quot;;
                }
            }
            else
            {
                Label1.Text = &quot;Enter Correct User Name&quot;;
            }
        }</pre>
<pre lang="sql">Use Forms Authentication for that
try this link
Link [^]</pre>
 
Share this answer
 
Comments
Dalek Dave 25-Mar-11 10:23am    
Good Call.
I think you need to read up a bit on authentication and how it works in an IIS environment. What you are asking for should not be handled only in code and SQL Server. You should incorporate built in IIS authentication processes to achieve your goal.
Check out some of these links.
 
Share this answer
 
Comments
Dalek Dave 25-Mar-11 10:23am    
Good Advice
fjdiewornncalwe 25-Mar-11 19:33pm    
Thanks, Dave.
Sergey Alexandrovich Kryukov 1-Apr-11 5:36am    
Good, my 5.

By the way, my congratulations with April 1st! :-)
Din't you see my April 1st post yet?
Please see this (and of course my "Answer"): http://www.codeproject.com/Questions/175233/WARNING-Black-Line-of-Death-in-windows-phone-7.aspx

:-)
--SA
You should read this:

Scott Gu's Blog Entry[^]
 
Share this answer
 
v2
Use Forms Authentication.
Use some thing like this at web.config

XML
<location path="secure">
    <system.web>
      <authorization>
        <deny users="?"/>
        <deny users="jhon"/>
      </authorization>
    </system.web>
  </location>

secure is a folder which contains your secure webforms.
XML
<authentication mode="Forms">
      <forms loginUrl="Default.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH"
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" >
        <credentials passwordFormat="Clear">
          <user name="kim" password="kim@123"/>
          <user name="jhon" password="jhonn"/>
        </credentials>
      </forms>
    </authentication>

Now at server side code

Default.aspx is your login form, Drag Two TextBoxes and a Button
at click event of button write following code. Default2.aspx is destination page. Secure is a folder which can have webforms which you wants to make secure

if 

(FormsAuthentication.Authenticate(

TextBox1.Text, TextBox2.Text))
        {
            

FormsAuthentication.SetAuthCookie(
                 

this.TextBox1.Text.Trim(), false);

            

FormsAuthenticationTicket ticket1 

=
               new 

FormsAuthenticationTicket(
                    1,             

                      // version
                    

this.TextBox1.Text.Trim(),   // 

get username  from the form
                    DateTime.Now,  

                      // issue 

time is now
                    

DateTime.Now.AddMinutes(10),       

  // expires in 10 minutes
                    false,      // 

cookie is not persistent
                    "HR"           

                   // role 

assignment is stored
                // in userData
                    );
            HttpCookie cookie1 = 

new HttpCookie(
              

FormsAuthentication.FormsCookieNam

e,
              

FormsAuthentication.Encrypt(ticket

1));
            

Response.Cookies.Add(cookie1);

            // 4. Do the redirect. 
            String returnUrl1;
            // the login is 

successful
            if 

(Request.QueryString["ReturnUrl"] 

== null)
            {
                returnUrl1 = 

"Default2.aspx";
            }

            //login not 

unsuccessful 
            else
            {
                returnUrl1 = 

Request.QueryString["ReturnUrl"];
            }
            

Response.Redirect(returnUrl1);

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900