ASP membership or facebook membership (working with db and process) - asp.net

I want to ask you for something:
I want do an IS on asp.net (MVC) with my custom membership connected to MSSQL
On DB will be more things and there will be a lot of connection with "userId"
But it's not a necessary to create NEW account just for this IS (but you can)...
But if you choose that you don't want create new account you can join with your FB.
But there is a problem... How create a design and working with my DB when you are just FB user and you want make some change (add some data to DB... And this data are connected to your account but it can be IS-account or FB, and of course I want to add users to roles, and with the FB? It's possible?)
Here is my part of DB which is responsible for memberships:
User:
id ( uniqueidentifier PK)
username (text)
password..
....
Role:
id ( uniqueidentifier PK)
name ( text)
UsersInRoles:
id ( uniqueidentifier)
roleID (uniqueidentifier (FK from Role) )
userID (uniqueidentifier (FK from User) )
And I am using an ID (PK from User table everywhere (in other tables) as signatures who add a new item and other)
Thank you.

Related

Teradata : Which dbc table contains the ACCOUNT field?

I need to find out what is my account . I looked at the following tables :
dbc.accounts
DBC.AccountInfoV
DBC.ProfileInfo
They have the accountname, which is not the same . WHere can I find my account ?
Thanks
Your currently active account can be retrieved using the built-in ACCOUNT function:
SELECT ACCOUNT;
Your default account is returned by:
SELECT DefaultAccount
FROM dbc.UsersV
WHERE UserName = USER;
And the list of all accounts your user might use, assigned either to the user directly or via the user's profile:
SELECT AccountName
FROM dbc.accountinfoVX
WHERE (PROFILE IS NULL AND USERNAME = USER)
OR (USERNAME = PROFILE)

SQL Return all rows if admin, otherwise return user records

When using SQL DataSource, I want to return the record essentially tied to the currently logged in user. I can do this, just fine.
But, when that logged in user is tagged as 'ADMIN' in the user table, I want to return all rows.
Fields:
Name
Address
databaseid
IsAdmin
ASP.net page sends #databaseid
SQL server returns Name and Address for #databaseid user only. Perfect!
but I want to return everything in the table if IsAdmin = 1 for that given databaseuserid.
I am using a SP to return records. Basically ignore the filter.
Can you modify the stored proc? If so, then change the select stmt:
SELECT Name, Address, databaseid, IsAdmin
FROM YourTableName
WHERE databaseid = #databaseid
OR 1 = (SELECT IsAdmin
FROM YourTableName
WHERE databaseid = #databaseid);

associating my own table to userProfile table in SqlMembership doesn't work as expected

I have this table named Person :
FirstName,LastName,BirthDate,UserId(FK,int,not null)
userId field is joint to userProfile table of SqlMembership
and 2 other tables named Category and News besides SqlMembership tables like UserInRole,... in a database
I create a .edmx file and add my own tables (Category,News,Person) to it.cause my approach is database first.
now I except when I get current logged in user,I can access it's related person:
if (Request.IsAuthenticated)
{
var context = new UsersContext();
var user = context.UserProfiles.SingleOrDefault(u => u.UserName == "asma");
ViewBag.fullName= user.person.FirstName+' '+user.person.LastName; //ERROR
}
but when I write user. the intellisense just gives me 2 fields:UserId and UserName
where is user.person....
on the other hand I cannot add UserProfile to my .edmx file.adding UserProfile to .edmx file causes 'MVC4 duplicated UserProfile table' problem
What's my fault?adding my own tables to the database that is created by SqlMembership?

Get the UserID of the current logged in User in asp.net default membership provider

I have two tables
Employee (EmpID, Name, UpdatedBy, UpdateDate)
EmpContact (ContactID, Contact,EmpID)
I am trying to create a trigger which update a Employee table fields UpdatedBy,UpdateDate. when contact is added or modified i want to update Employee's UpdatedBy and UpdateDate fields (These two tables have different asp.net-mvc views).
I am using default membership provider to authenticate the user. So my question is how can i get the current logged in user in trigger who initiated the insert, update or delete. Is there any way i can know which asp.net user id initiated the transaction within the TRIGGER
Create an insert/update/delete trigger that updates the Emplyoee table when records in EmpContact are created/updated/deleted, for example(insert):
CREATE TRIGGER [dbo].[trgCreateEmpContact] ON [dbo].[EmpContact]
FOR INSERT
AS
UPDATE dbo.Employee
SET UpdateDate=GetDate(), UpdatedBy = I.UpdatedBy
FROM Employee E
INNER JOIN Inserted I ON E.EmpID = I.EmpID
So you need to add the UpdatedBy column to your EmpContact table too.
You get the current logged in user in the following way:
MembershipUser currentUser = Membership.GetUser();
GUID currentUserID = (GUID)currentUser.ProviderUserKey;
Another(better) approach would be to create a stored-procedure that internally creates the EmpContact record and updates the Employee table in a transaction, for example(untested):
CREATE PROCEDURE [dbo].[InsertEmpContact]
#ContactID int OUTPUT,
#Contact varchar(50) OUTPUT,
#EmpID int OUTPUT,
#UpdateDate datetime OUTPUT,
#UpdatedBy int OUTPUT,
with execute as Owner
AS
BEGIN TRANSACTION
INSERT INTO EmpContact(Contact, EmpID)
VALUES (#Contact,#EmpID)
;SELECT #ContactID=ContactID,#Contact=Contact,#EmpID=EmpID,#UpdateDate=GetDate()
FROM EmpContact WHERE (ContactID = SCOPE_IDENTITY())
;UPDATE dbo.Employee
SET UpdateDate=#UpdateDate, UpdatedBy = #UpdatedBy
WHERE EmpID = #EmpID
IF ##ERROR <> 0
BEGIN
-- Rollback the transaction
ROLLBACK
RETURN
END
COMMIT
If you want to detect the current logged in user from memebership please use these links
http://www.codeproject.com/KB/aspnet/CustomMembershipProviders.aspx
http://theintegrity.co.uk/2010/11/asp-net-mvc-2-custom-membership-provider-tutorial-part-2/
It depends on how your app connects to your database. If you do it, like almost all of us, with connection pooling and a single user connecting to the database, the answer is no, you cannot know that in the trigger.
E.g. the app connects to the database as sa/pwd (not advisable, but for arguments sake only).
If user one (Paul) connects to the db and does an update, your trigger will get sa as user.
If user two (Anne) connects to the db, the trigger will also get sa as user.
So you need to include the update of the user in the update statmement (or as an argument to your stored procedure)

Entity Framework: Working with FK's (why do they get hidden?)

First of all, let's define a few tables:
Users table will store information about a user:
Users
- UserID (int, PK)
- ...
UserTasks is a table that stores a task associated with a user:
UserTasks
- userID (int, FK to Users table)
- taskName (varchar)
When I generate the UserTasks table using the ADO Entity Framework, I'll get a class that looks like this:
UserTasks
- taskName (string)
- Users (collection of Users objects)
Note: There's no userID that is generated in the UserTasks table. So let's assume now that I need to insert a new user task... how do I do it? I don't have access to the userID FK field, so my only option is to do a lookup and populate a Users object, then pass that to my task object, like this:
//get the user we are interested in
var user = (from u in context.Users
where u.userID == 2
select u).FirstOrDefault();
//create the new task
UserTasks newTask = new UserTasks();
newTask.taskName = "New Task";
newTask.User = user;
The problem with the above is that I'm doing an extra and needless database call to populate my user object. Is there anyway to somehow create a new mapping to my userID field and still keep the User object?
Thanks
--Ryan
Not now, but maybe in the next version.

Resources