Showing posts with label Web Browser. Show all posts
Showing posts with label Web Browser. Show all posts

Tuesday, July 19, 2011

Send Email In Asp.net Using Gmail

We can use Gmail smtp server to send email, using Asp.Net. Before we start developing Asp.net code, make sure POP is enabled in your Gmail's mail setting.






Once you done with this step SAVE your Gmail settings....

To do Email sending in Asp.net we need "System.Net.Mail" namespace.

public static Boolean SendEmail()
{
  MailMessage mail = new MailMessage();

mail.From = new MailAddress("YOUR FROM EMAIL ADDRESS");
mail.To.Add(new MailAddress("YOUR TO EMAIL ADDRESS"));            
mail.Bcc.Add(new MailAddress("YOUR BCC EMAIL"));
            
  mail.Subject = "Email using Gmail";

  string Body = "Put your Email Body TEXT here"; 
// Here you can put HTML string if you want email to be sent in HTML format.
  mail.Body = Body;

  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
  smtp.Credentials = new System.Net.NetworkCredential
       ("Username@gmail.com","GmailPassword");
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;
  smtp.Send(mail);
}

This is simple function to send email in Asp.net using Gmail account.
You can also find Copy of this email in Your SENT items. Submit this story to DotNetKicks

Read more...

Wednesday, March 16, 2011

Active Directory Authentication Asp.net

Very offen in web development, we come across a requirement where we need to use Active Directory Authentication in Asp.net; i .e. we need to use Active Directory to Authenticate user. The flow for the same can be assumed as following point. In this case we shall not have User table in Database unless required, we can verify the Authenticity of user with Active Directory.
  1. User logs in with Active Directoyl Login and Password.
  2. System verifies Login and Password with Active Directory
  3. If we get Success we will proceed the login else will display error.

We shall use following C# class naming AuthenticateUser to implement ActiveDirectory Authentication in Asp.net

public class AuthenticateUser
{
    public AuthenticateUser()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private string _path;
    private string _filterAttribute;

    public AuthenticateUser(string path)
    {
        _path = path;
    }

    public bool IsAuthenticated(string domain, string username, string pwd)
    {
        string domainAndUsername = domain + @"\" + username;
        DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

        try
        {
            //Bind to the native AdsObject to force authentication.
            object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);

            search.Filter = "(SAMAccountName=" + username + ")";
            search.PropertiesToLoad.Add("cn");
            SearchResult result = search.FindOne();

            if (null == result)
            {
                return false;
            }

            //Update the new path to the user in the directory.
            _path = result.Path;
            _filterAttribute = (string)result.Properties["cn"][0];
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }

        return true;
    }


}

Remeber we have added "System.DirectoryServices" name space.

We can simply use following code to Authenticate user using ActiveDirectory in asp.net

Note: In following code we use Authentication on Page Load event, you can implement this on Login Page.

protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser cls = new AuthenticateUser("LDAP://YourActiveDirectoryPath"); //Set Active Directory Path
        bool flag = cls.IsAuthenticated("ZESTORMTPL", "UserLoginID", "Password");
        //Put your UserLoginID and Password
    }
Active Directory Authentication Asp.net is as simple as copy pasting above code and use it as it is.
Submit this story to DotNetKicks

Read more...

Friday, January 14, 2011

Display Loading Image in Asp.net

To display loading image we shall use following JavaScript. There are many ways to display loading, but I feel this is the simple to use and implement. We just need one div tag with position set to absolute, and put loading image inside the div tag. Check out following code.

Loading...

(Make sure you set z-index if required.)
In above I have just used text "Loading...", instead of this you can put loading images.Put this div tag as first element of FORM tag. And after above div tag immediately put following JavaScript 



And call function "init()", on "onload" event of body tag.

Done, now as your page loads you can see "Loading..."... Submit this story to DotNetKicks

Read more...

Thursday, October 28, 2010

Display your logo on address bar

Many times to make our site standout, we need to display our logo near address bar. There is very simple JavaScript which takes care of this. We need to put following code in tag HEAD tag.






For each page of the website we need to add above code.
demoico.ico is an icon file which we can use to display near address bar.
We can also manipulate Header Section Dynamically, and change the icon for different pages. Submit this story to DotNetKicks

Read more...