(i saw Demo Dirt - but it use for HTTP Web and i Don't know how switch it to TCP/IP) I'm new from Delphi - I need help about use DataSnap and Client Login form (check user password from SQL Server) then apply role. Any code or answer wil开发者_如何学JAVAl be appreciate Thank in advance
I'd be happy to help you if you could explain a bit more clearly what you want to do and what currently isn't working. (What you are trying to accomplish with the application, What you have so far for authentication, and what exactly you are stuck on.)
If you want to start from scratch, this is what you'd do:
Use the "DataSnap Server" wizard to create a new server project. This will use Indy (Instead of WebBroker) and will allow for TCP/IP connections. (even for Heavyweight callbacks.)
While setting the properties in the wizard, make sure to check the "Authentication" option ("Authorization" isn't required. Only if you want a more complicated authentication/authorization mechanism.)
Open the ServerModule unit the wizard generated, and switch to the code tab
Find the "DSAuthenticationManager1UserAuthenticate" function (added automatically if you checked Authentication in the wizard.)
In this function, do your authentication... you have access to the user name ("User") and password ("Password"). Connect to a database, and see if there is a matching user, and what roles the user has. Then, populate the UserRoles collection passed in to this method with all the roles the user has.
Once you specify roles on a user, those do nothing until your server methods also have allowed or denied roles assigned to them. You can do this in the TDSAuthenticationManager component at design time in the Object Inspector. (ServerModule form). There is a Roles collection you can modify.
Or, you can add attributes in code to your server methods. You can put this for example:
[TRoleAuth('admin')] function EchoString(String: Value): String;
That will mean EchoString can only be invoked by a user with the 'admin' role.
Or this:
[TRoleAuth('', 'admin')]
function EchoString(String: Value): String;
Which would mean anyone EXCEPT a user with the 'admin' role can invoke EchoString.
Or this:
[TRoleAuth('admin,guest,visitor')]
function EchoString(String: Value): String;
Which would mean any user with 'admin', 'guest', or 'visitor' as one of their roles (or any combination) can invoke EchoString.
NOTE: To use the TRoleAuth attribute, you need to add the DSAuth unit to your uses clause, otherwise the role attributes will be ignored.
On the client, add a TSQLConnection component. Set the driver to "Datasnap" then expand it. Set the host and port values. See more on setting this up here:
http://blogs.embarcadero.com/mathewd/2011/07/12/programmatically-set-datasnap-credentials-on-a-tsqlconnection/
Also, I've previously posted on my blog about authentication: http://blogs.embarcadero.com/mathewd/2010/09/12/authentication-and-authorization-with-datasnap-and-rest/
I hope that helps.
Mat
精彩评论