These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: using nhibernate to get login user..
PostPosted: Tue Dec 30, 2008 4:27 pm 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
hello. im trying to get a user with a role for my login. this is the first time i use nhibernate for this.. i wonder how to do this both so i can get a bool and have the admin object so i can use the admin.role for later use when i set the role to the user.. i dont know if you know what i mean, if not ill try to explain agin.. check my code and please point out things i could do better and if there is a way to return the admin to and the bool..


Code:
try
                {
                    ICriteria crit = _session.CreateCriteria(typeof (Admin))
                        .Add(Expression.Eq("Name", userName))
                        .Add(Expression.Eq("Pwd", password));
                    Admin admin = crit.UniqueResult<Admin>();
                   
                    if (admin != null)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                    _session.Flush();
                    tx.Commit();
                }
                catch (NHibernate.HibernateException)
                {
                    tx.Rollback();
                    throw;
                }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 10:38 am 
Newbie

Joined: Sat Nov 29, 2008 6:59 pm
Posts: 19
Location: Burlington, VT
Without fully understanding what you're doing, here's the free advice you asked for:

- Your application may not be secure; the password should never be stored as text.

- What's the purpose of the transaction in this code snippet? There's no transaction in a select. Similarly, I wouldn't expect to see a Flush in an authentication method.

- If your object model has Admin inheriting from User, then you should return a User from your authentication method. If it's null then authentication failed. If it's non-null then the calling code can check to see if an Admin type was returned. Alternately, if your User object has a collection of roles or a role property then the calling code can check that.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 11:08 am 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
well i didnt write the topic title right.. the point of this is i got a admin login, now they got roles of access.. what i want to do is just a simple login select.. but i want to be able to save the role of the admin in a authicationticket later on..

this gets that the admin is valid and return a bool.. what im tryn to figure how to do so get the admin that is valid, so i can access its role in the codebehind? you know how i mean? this dont return the admin as a admin objec just a bool...

im a newbie at nhibernate so i use the flush for all i do.. the vid i seen does it so i asume that was how it was done.. ill look at it more then..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 12:08 pm 
Newbie

Joined: Sat Nov 29, 2008 6:59 pm
Posts: 19
Location: Burlington, VT
OK, I think you have two options:

1. Return the Admin object instead of a bool and check if it's null in the calling code.

2. Refactor the method to pass it an Admin object as an out parameter, similarly to the framework TryParse methods:
public bool TryAuthenticateAdmin(out Admin admin) { ... }

Flush is called when you are done with a session and want to persist changes to the data store. If there are no changes then there's nothing to persist.

Konnekt wrote:
well i didnt write the topic title right.. the point of this is i got a admin login, now they got roles of access.. what i want to do is just a simple login select.. but i want to be able to save the role of the admin in a authicationticket later on..

this gets that the admin is valid and return a bool.. what im tryn to figure how to do so get the admin that is valid, so i can access its role in the codebehind? you know how i mean? this dont return the admin as a admin objec just a bool...

im a newbie at nhibernate so i use the flush for all i do.. the vid i seen does it so i asume that was how it was done.. ill look at it more then..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2009 6:24 pm 
Newbie

Joined: Tue Nov 18, 2008 11:16 am
Posts: 19
yea ill go with the option 2.. thx for your time and help helped me out alot..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.