locked
Login Check RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have the below Web.Config. I want when user runs application Login Page should open . If Username,Password ok then Default.aspx page should open. Attached Code should work or some changes required.

    Secondly if user directly enter http://localhost:1560/Default.aspx in URL user should be redirected to Login Page .

    <system.web>
        <authentication mode="Forms">
          <forms loginUrl="login.aspx" 	protection="All" 	timeout="1440" />
    		</authentication> 
    
        <authorization>
          <deny users="?" /> 
        </authorization>
        
        <compilation debug="true" targetFramework="4.6"/>
        <httpRuntime targetFramework="4.6"/>
      </system.web>
      <location path="Login.aspx">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>

    int id;
                BAL.UsrLogin __objectdata = new BAL.UsrLogin();
                id = __objectdata.chkLogin(usercode, pwd);
                if (id == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                } </location>

    Thanks

    Friday, January 8, 2016 9:48 AM

All replies

  • User541108374 posted

    Hi,

    you can't add C# code in your web.config.

    Grz, Kris.

    Friday, January 8, 2016 10:22 AM
  • User-1499457942 posted

    Hi

       That is the Web Method

    Thanks

    Friday, January 8, 2016 10:26 AM
  • User1738843376 posted

    You should be addin the 'DefaultUrl' property to the 'forms' tag:

    <forms loginUrl="login.aspx" defaultUrl="default.aspx" protection="All" timeout="1440" />
    

    and you dont need the 'Location' tag that you're using to allow access to the login.aspx page, since it was declared on the forms.authentication.

    also, after using the webmethod to check if the user/password pair is valid, you should call the following code:

    FormsAuthentication.RedirectFromLoginPage(TheUsersNameGoesHere, True)

    This latest line will ensure that the authentication is done, and that the user gets redirected to the default page once its authenticated

    Friday, January 8, 2016 12:06 PM
  • User475983607 posted

    I have the below Web.Config. I want when user runs application Login Page should open . If Username,Password ok then Default.aspx page should open. Attached Code should work or some changes required.

    Secondly if user directly enter http://localhost:1560/Default.aspx in URL user should be redirected to Login Page .

    Your question is unclear.  Are you having an issue?  What are the steps to reproduce the issues, what are the expected outcomes and what actually happens?

    Friday, January 8, 2016 1:13 PM
  • User-1499457942 posted

    Hi Obelix

      If i do

    <system.web>
        <authentication mode="Forms">
          <forms loginUrl="login.aspx" 	protection="All" 	timeout="1440" />
    		</authentication> 
    
        <authorization>
          <deny users="?" /> 
        </authorization>
        
        <compilation debug="true" targetFramework="4.6"/>
        <httpRuntime targetFramework="4.6"/>
      </system.web>
      <location path="Login.aspx">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
    
    $.ajax({
                            type: "POST",
                            url: "Login.aspx/chkLogin",
                            data: "{'usercode':'" + usercode + "', 'pwd': '" + pwd + "'}",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            success: function (response)
                            {
                                switch (response.d) {
                                    case 1:
                                        window.location.href = "Default.aspx";
                                        break;
                                    default:
                                        alert(response.d);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert("Status: " + textStatus); alert("Error: " + XMLHttpRequest.responseText);
                            }
                        });
    
    public static int chkLogin(string usercode, string pwd)
            {
                int id;
                BAL.UsrLogin __objectdata = new BAL.UsrLogin();
                id = __objectdata.chkLogin(usercode, pwd);
                return id;
            }


    n't Use Location method i get this error - {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

    Thanks

    Saturday, January 9, 2016 4:27 AM
  • User1738843376 posted

    JagjitSingh

    <forms loginUrl="login.aspx" protection="All" timeout="1440" />


    Add  defaultUrl="Default.aspx" and try removing both the location tag (just insert location tags for the folders where you store images that show on the page and also the CSS and JS files) and the AJAX method, and do the post via VB/C# and check if it works

    Wednesday, January 13, 2016 12:56 PM
  • User-1499457942 posted

    Hi

      I have written the below code but it is giving message , True does not exist in the current context

    Thanks

    [WebMethod]
            public static int chkLogin(string usercode, string pwd)
            {
                int id;
                BAL.UsrLogin __objectdata = new BAL.UsrLogin();
                id = __objectdata.chkLogin(usercode, pwd);
                if (id == 1)
                {
                    FormsAuthentication.RedirectFromLoginPage(usercode, True);
                    return 1;
                }
                else
                { 
                    return 0;
                }
            }

    Wednesday, January 20, 2016 4:10 PM
  • User1738843376 posted

    That is odd... i usualy work in VB, so maybe you need to use 1 instead of true in c# in this context...

    Check out the MS page on this item:

    https://msdn.microsoft.com/en-us/library/ka5ffkce(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    Wednesday, January 20, 2016 4:39 PM
  • User-1499457942 posted

    Hi Obleix

      I have created another thread Form Authentication . When i try like that it gives error.

    Wednesday, January 20, 2016 4:44 PM
  • User-219423983 posted

    Hi JagjitSingh

    About the error message “Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}”, you could have a look at the following links that have this similar error.

    http://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized?answertab=votes#tab-top

    http://stackoverflow.com/questions/3182652/asp-net-webmethod-always-returns-401

    About using Form authentication, you could have a look at the following demo. About the new error message, could you share it and then we could better help you?

    http://www.aspsnippets.com/Articles/Simple-Form-based-authentication-example-in-ASPNet.aspx

    Best Regards,

    Weibo Zhang

    Thursday, January 28, 2016 6:30 AM