Nannette Thacker ShiningStar.net

ASP.net Web Application Development

Sponsors

News

See all Blog Posts by Nannette.

Nannette Thacker, consultant and owner of Shining Star Services LLC, specializes in development of custom dynamic database driven web applications utilizing ASP.net technologies. Nannette has been developing ASP sites since 1997. Nannette has written numerous articles on web development techniques and tutorials.

Nannette is the owner and developer of ChristianSinglesDating.com.

 Subscribe in a reader





View Nannette  Thacker's profile on LinkedIn

'X' is ambiguous in the namespace

I upgraded my projects with the latest version of a third party control (happened to be Telerik, but I've seen the problem occur other times as well) and got the ominous error on the BUILD:

 'Filename' is ambiguous in the namespace.

The path led to the temporary folders directory. I've read a post where someone deleted all the files in the directory. But that was after I already fixed it.

 The fix is simple, use REBUILD instead of BUILD.

 BTW, I'm using Visual Studio 2010 and Framework 4.0.

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn

Google Chrome Loses ASP.NET Sessions - Need FavIcon

I had programmed a brilliant web page in ASP.NET 4.0 and lo and behold, this one page lost its sessions in Google Chrome. I could run it locally in debug and could not reproduce the issues in Chrome. Didn't happen in IE or Firefox, only Chrome on the published server.

I finally found in a forum where someone mentioned that Google Chrome looks for favicons and if it doesn't find one it will throw a 302 redirect and kill the session.

 http://stackoverflow.com/questions/8247842/session-data-lost-in-chrome-only

"The issue could be your server is looking for favicons, if it is not found the server throws out a 302 redirect, which kills the session variables."

 So I went to Google and searched for a favicon generator, found one here at http://www.favicon.cc/

Uploaded a file which they turned into a favicon.

Then in my MasterPage within the HTML head tag and below the title tag I added the favicon code:

<link rel="shortcut icon" type="image/x-icon" href="~/Site/Images/favicon.ico" />

Published the code and checked it out and it fixed it!

Here's hoping this helps you with your Chrome Nightmares!

 

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn

Posted: Nov 09 2012, 02:51 PM by nannette | with no comments |
Filed under:
4 Year Anniversary Posting on Weblogs.Asp.Net!

On January 24th, 2012 I celebrate my 4th year of writing on this blog. I have written over 140 posts and have received over 700 comments.

Thank you readers for your support!

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn

ChangePassword Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the ChangePassword method. I welcome any suggestions for improvement.
public override bool ChangePassword(string username, 
    string oldPassword, string newPassword)
{
    try
    {
        byte[] hashedNewPassword = HashPassword(newPassword.Trim());
        byte[] hashedOldPassword = HashPassword(oldPassword.Trim());
        using (var context = new SSSEntities())
        {
            UserProfile u = context.UserProfiles
            .SingleOrDefault(up => up.UserName == username && 
                up.UserPassword == hashedOldPassword);
            if (u != null)
            {
                u.UserPassword = hashedNewPassword;
                context.SaveChanges();
                return true;
            }
            else
                return false;
        }
    }
    catch (InvalidOperationException ex)
    {
        throw ex;
    }
    catch (ArgumentException)
    {
        throw;
    }
}

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn
GetUser Methods for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the GetUser methods. I welcome any suggestions for improvement.
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
    using (var context = new SSSEntities())
    {
        UserProfile u = context.UserProfiles
                    .SingleOrDefault(up => up.UserId == Convert.ToInt32(providerUserKey));
        MembershipUser membershipUser = GetMembershipUser(u);
        return membershipUser;
    }
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
    using (var context = new SSSEntities())
    {
        UserProfile u = context.UserProfiles
            .SingleOrDefault(up => up.UserName == username);
        MembershipUser membershipUser = GetMembershipUser(u);
        return membershipUser;
    }
}
// custom method to return a UserProfile
public UserProfile GetUser(string username)
{
    using (var context = new SSSEntities())
    {
        UserProfile u = context.UserProfiles
            .SingleOrDefault(up => up.UserName == username);
        return u;
    }
}
// custom method to return a UserProfile
public UserProfile GetUser(object providerUserKey)
{
    using (var context = new SSSEntities())
    {
        UserProfile u = context.UserProfiles
                    .SingleOrDefault(up => up.UserId == Convert.ToInt32(providerUserKey));
        return u;
    }
}


May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn
Custom Error Pages and 404 Page Not Found Error Web.Config Setup

When receiving 404 page not found errors on your web site, and you wish to direct users back to your home page, you need to setup two things in your web.config to handle all instances.

If you use IIS7 and go to the Error Pages section and setup the 404 error to "Execute a URL on this site" and have it go to : /default.aspx and select OK to save the changes, it will update your system.webServer section of your web.config file and add these lines:

<httpErrors>
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath="" 
path="/default.aspx"
responseMode="ExecuteURL" /> </httpErrors>

This will redirect any pages such as http://www.shiningstar.net/test with no file extensions. But if the URL sends the user to a page with a known file extension, they will still be taken to the 404 error page.

So if you want to redirect a page such as http://www.shiningstar.net/test.aspx you will also need to add custom error handling to your system.web section of your web configuration file:

<customErrors mode="RemoteOnly" 
defaultRedirect="~/ErrorPages/Error.aspx"> <error statusCode="404" redirect="~/default.aspx"/> </customErrors>


The 3 options for the mode are: Off, On, and RemoteOnly. To test your error handling locally, change the mode to "On." Once you've tested it, change it to "RemoteOnly" which will still allow you to receive descriptive errors when developing locally.

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn

CreateUser Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the CreateUser method. I welcome any suggestions for improvement.
public override MembershipUser CreateUser(string username, string password,
    string email, string passwordQuestion, string passwordAnswer,
    bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
    // only first 3 fields are passed in from the AccountModels.cs
    try
    {
        status = UsernameExists(username);
        if (status == MembershipCreateStatus.DuplicateUserName)
        {
            return null;
        }
        status = DuplicateEmail(email);
        if (status == MembershipCreateStatus.DuplicateEmail)
        {
            return null;
        }

        byte[] hashedPassword = HashPassword(password.Trim());
        GetValues gv = new GetValues();
        string ipAddress = gv.getIPAddress();
        int userStatus = 
            Convert.ToInt32(SSS.GlobalListValues.Enums.UserStatusCode.Active);

        using (var context = new SSSEntities())
        {
            UserProfile newUser = new UserProfile()
            {
                Email = email,
                UserPassword = hashedPassword,
                UserName = username,
                DateCreated = DateTime.Now,
                DateUpdated = DateTime.Now,
                DatePasswordLastChanged = DateTime.Now,
                DateLastLogin = DateTime.Now,
                UserStatusCode = userStatus,
                IpAddress = ipAddress,
            };


            // insert the User Role
            int userRole = 
                Convert.ToInt32(SSS.GlobalListValues.Enums.UserRoleCode.User_Public);
            // look up the desired user role : 
            // uses a UserRole join table with a many to many relation
            // between the UserProfile table and the ListValue table
            ListValue ur = context.ListValues
                .SingleOrDefault(lv => lv.ListValueId == userRole);
            newUser.UserProfileUserRoles.Add(ur);

            context.UserProfiles.AddObject(newUser);
            context.SaveChanges();

            // NKT: after creation, go back and retrieve the auto-generated identity key and 
            // update the userId's for the created and updated userId
            int userId = newUser.UserId;
            newUser.CreatedUserId = userId;
            newUser.UpdatedUserId = userId;

            context.SaveChanges();
            status = MembershipCreateStatus.Success;
            return GetMembershipUser(newUser);
        }
    }
    catch (ArgumentException)
    {
        status = MembershipCreateStatus.ProviderError;
        return null;
    }

}

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn
DeleteUser Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the DeleteUser method. I welcome any suggestions for improvement.
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
    // deleteAllRelatedData not implemented
    try
    {
        using (var context = new SSSEntities())
        {

            UserProfile u = context.UserProfiles
                .SingleOrDefault(up => up.UserName == username);

            context.UserProfiles.DeleteObject(u);
            context.SaveChanges();
            return true;
        }
    }
    catch
    {
        return false;
    }


May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn
UsernameExists & DuplicateEmail Helper Methods for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the UsernameExists & DuplicateEmail Helper methods. I welcome any suggestions for improvement.
// helper method
public MembershipCreateStatus UsernameExists(string username)
{
    using (var context = new SSSEntities())
    {
        if (context.UserProfiles.Any(
                    u => u.UserName == username))
        {
            return MembershipCreateStatus.DuplicateUserName;
        }
        return MembershipCreateStatus.Success;
    }
}

// helper method
public MembershipCreateStatus DuplicateEmail(string email)
{
    using (var context = new SSSEntities())
    {
        if (context.UserProfiles.Any(
                    u => u.Email == email))
        {
            return MembershipCreateStatus.DuplicateEmail;
        }
        return MembershipCreateStatus.Success;
    }
}

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn
GetMembershipUser Helper Method for Entity Framework MVC3 Razor Custom Member Provider C# Using LINQ
From my C# MVC3 Razor Custom Membership Provider article and source code, here is the code for the GetMembershipUser Helper method. I welcome any suggestions for improvement. Since we are working with a UserProfile Entity and the Membership Provider overridable methods require we return a MembershipUser type, below we create our MembershipUser type using values from our UserProfile type so we can return the required type in our methods.
// helper method
public MembershipUser GetMembershipUser(UserProfile u)
{
    // copy pertinent UserProfile data to the MembershipUser 
// data to be returned as a MembershipUser type object userIDObj = u.UserId; MembershipUser membershipUser = new MembershipUser( this.Name, u.UserName, userIDObj, u.Email, string.Empty, string.Empty, true, false, (DateTime)u.DateCreated, (DateTime)u.DateLastLogin, (DateTime)u.DateUpdated, (DateTime)u.DateLastLogin, (DateTime)u.DateLastLogin); return membershipUser; }

May your dreams be in ASP.NET!

Nannette Thacker


View Nannette  Thacker's profile on LinkedIn
More Posts Next page »