Skip to content

Authentication code in C#

Shamseer

Asked Jul 21, 2009, 5:13 AM EDT1 answer
Share
LinkedInX
HI,

I need to code in C# to input a username and password for an authentication dialog box in Windows while trying to access a share in another system. The idea here is to pass the username and password in a function as argument so that this function will automatically enter both (username and password) once the Windows dialog box for Aithentication pop ups while accessing a share in any other system.
Can you please help me out with any API or any other piece of code to work out!:)

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Read this

    http://asp-tech.blogspot.com/2009/02/form-authentication.html

    Some more information




        protected void LoginAuthenticate(object sender, AuthenticateEventArgs e)
        {


    if username and pwd given by the enduser is available in the database then we can authenticate the user by providing

                            e.Authenticated = true;
                            AuthenticateUser();

        }


    protected void AuthenticateUser(string userName)
    {
      /*Providing the FormsAuthenticationTicket "Ticket" to the authenticated
      user which contains version, username, entry datetime and expire date time,
      userdata and cookie path*/
      FormsAuthentication.Initialize();
      FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket
                   (1, userName.Trim(), DateTime.Now, DateTime.Now.AddMinutes(90),
                    true, userName.Trim() + "," + Page.ClientID,
                    FormsAuthentication.FormsCookiePath);

      //encrypt the ticket and assign it into the string variable "hash"
      string hash = FormsAuthentication.Encrypt(Ticket);

     /*FormsAuthentication.FormsCookieName --> This is the name
         that we gave in web.config file*/

      HttpCookie Cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
      Response.Cookies.Clear();

      /*checking whether the cookie is constant,
        If constant then expires the cookie based on the expiration
        time of user specified in ticket
     */
      if (Ticket.IsPersistent)
      {
          Cookie.Expires = Ticket.Expiration;
          Response.Cookies.Add(Cookie);
      }
    }


    ==================================================================
    Clear cookie when user Logout


    Response.Cookies.Remove(FormsAuthentication.FormsCookieName);

    Removes the FormsAuthentication ticket from the server


    ====================================================================
    Get Windowns userName

    string str = Request.ServerVariables["LOGON_USER"];


    ====================================================================

    Like gmail if the user didn't logout while closing the browser of shutdown,
    it automatically login when user request any page.

    void Session_Start(object sender, EventArgs e)
    {

            if (Request.Cookies["UserInfo"] != null)
        {
                System.Collections.Specialized.NameValueCollection UserInfoCookieCollection;
                UserInfoCookieCollection = Request.Cookies["userInfo"].Values;
               
                HttpCookie CIDCookie = (HttpCookie)Request.Cookies["Customer"];
               
                Session["CustomerEmail"] = Server.HtmlEncode(UserInfoCookieCollection["CustomerEmail"]);
                Session["CustomerID"] = Server.HtmlEncode(UserInfoCookieCollection["CustomerID"]);
            }     
        }



       
       


    We have to give the path="/"  in web.config file. Then only it will work for Ie and Firefor.
    i.e. we logged in IE and open the page in Firefox it onlce again ask to logon. To over come this give the path.


    The place where cookies are stored in windows XP.
    This for IE

    C:\Documents and Settings\\Cookies


    If you set form authentication it give the user name that you gave in Login control

    other wise it took as windows authentication and gave windows user name
    string webUsername = HttpContext.Current.User.Identity.Name;
    0