locked
Custom form authentication, Fail to retrieve user from LoadUser() RRS feed

  • Question

  • Dear all,

    I have implemented custom form authentication as follows:

    	[EnableClientAccess]
    	public class IGLAuthenticationDomainService : LinqToEntitiesDomainService<GLEntities>, IAuthentication<User>
    	{
    		private static User DefaultUser = new User()
    		{
    			Admin = 0,
    			Name = string.Empty,
    			Roles = null,
    			Password = string.Empty,
    			UserName = string.Empty,
    		};
    
    
    		#region IAuthentication<User> Members
    
    		public User GetUser()
    		{
    			if ((this.ServiceContext != null) &&
    				 (this.ServiceContext.User != null) &&
    				 this.ServiceContext.User.Identity.IsAuthenticated)
    			{
    				return this.GetUser(this.ServiceContext.User.Identity.Name);
    			}
    			return DefaultUser;
    		}
    
    		public User Login(string userName, string password, bool isPersistent, string customData)
    		{
    			if (this.ValidateUser(userName, password))
    			{
    				User temp = new User();
    				temp.Name = userName;
    				temp.UserName = userName;
    				temp.Roles = null;
    				FormsAuthentication.SetAuthCookie(userName, isPersistent);
    
    				return temp;
    			}
    
    			return null;
    		}
    
    		private bool ValidateUser(string userName, string password)
    		{
    			using (var context = new GLEntities())
    			{
    				var login = from l in context.users
    								where l.user_name == userName && l.password == password
    								select l;
    
    				if (login.Count() == 1)
    					return true;
    
    				return false;
    			}
    			//return true; // for testing always return true
    		}
    
    		public User Logout()
    		{
    			FormsAuthentication.SignOut();
    			return DefaultUser;
    		}
    
    		public void UpdateUser(User user)
    		{
    			//throw new NotImplementedException();
    		}
    
    		#endregion
    
    		private User GetUser(string userName)
    		{
    			User user = new User();
    			user li = this.ObjectContext.users.First(u => u.user_name == userName);
    
    			user.Admin = li.admin;
    			user.UserName = li.user_name;
    			user.Password = li.password;
    			return user;
    		}
    	}
    
    
    	public class User : UserBase
    	{
    		// NOTE: Profile properties can be added here 
    		// To enable profiles, edit the appropriate section of web.config file.
    		// public string MyProfileProperty { get; set; }
    		public int? Admin { get; set; }
    		public string Password { get; set; }
    		public string UserName { get; set; }
    	}
    

     

    I can call the login() function [assign the IsPrestence to true] and it is responding as excpected, however when I try to call the loaduser() it always fail to retrieve the current user.

    Here is my call to loaduser()

    		private void Application_Startup(object sender, StartupEventArgs e)
    		{
    			((WebAuthenticationService)WebContext.Current.Authentication).DomainContext = new IGLAuthenticationDomainContext();
    			Resources.Add("WebContext", WebContext.Current);
    			WebContext.Current.Authentication.LoadUser(OnLoadUser_Completed, null);
    
    			InitializeRootVisual();
    
    		}
    
    		private void OnLoadUser_Completed(LoadUserOperation operation)
    		{
    			if (!operation.HasError)
    			{
    
    				if (operation.User.Identity.IsAuthenticated)
    				{
    					MessageBox.Show("User Already Found !!" + operation.User.Identity.Name);
    				}
    				else
    				{
    					MessageBox.Show("User NOT Found !!");
    				}
    			}
    			else
    			{
    				operation.MarkErrorAsHandled();
    				MessageBox.Show(operation.Error.Message, "ERROR",MessageBoxButton.OK);
    			}
    		}
    

    I have verfied that the cookie is created.

    What could cause such behavior.


    Best regards

    Waleed

    • Moved by Sheldon _Xiao Monday, July 11, 2011 7:47 AM (From:Windows Presentation Foundation (WPF))
    Thursday, July 7, 2011 5:30 PM