Referencing UserClaims in an Entity Framework Core class - asp.net-core

I'm developing a data project with ASP.NET Core and Entity Framework Core. We're starting to use user claims (Identity) to manage authorization for things like managers etc. Previously, we had a separate table for managers, so that each permission could be set on each object and we looked it up as required.
Simple example prior to UserClaims:
Store { StoreId, Name etc... plus ICollection<ManagerPermissions> }
User { StoreId, Name, Email etc}
ManagerPermissions { StoreId, UserId }
Using the separate ManagerPermissions table meant it was easy to list the relevant manager(s) for that store by using a navigation property like ICollection<ManagerPermissions>. If writing a query, I could do _context.Store.Include(s => s.ManagerPermissions) to include a list of managers to display.
Now that we're using user claims, I haven't found an easy way to do this. I've extended IdentityDbContext so the claims data is held within the same database as the other data.
I have seen
UserManager.GetUsersWithClaimAsync({ ClaimType="ismanager", ClaimValue = StoreId})
but this requires an instance of userManager and also a separate roundtrip to the database. The only other way I could think of is by doing a join on UserClaims table, but I'm not sure the most efficient way of doing this.

Related

Use non standard types in Azure Easy Tables

I'm trying to use Azures Easy Tables in my Xamarin Application.
I have data in 2 tables in azure which i added through csv. These tables include only standard types like string, date and numbers and are correctly synced to the device.
Additionally i have several other tables which are present in the local sync store. These tables / classes also use non standard types i defined myself. For example i habe a class Contact which has a property Company, which again is another class with several standard properties. These tables are currently only saved locally in the sync store, and it's working as expected. I'm now trying to sync these tables to to Azure Easy Tables.
So what i did was creating the table as an Azure Easy Table in Azure named Contact, with dynamic schema activated. When i try to Client.SyncContext.PushAsync() i get the following Exception:
MobileServiceLocalStoreException:
"Push operation has failed. See the PushResult for details."
Inner Exception / JsonReaderException:
"Failed to read errors from the local store."
Inner Exception / JsonException
"Unexpected character encountered while parsing value: C. Path '', line 0, position 0."
Push Result seems to provide no information (Error Count 0)
I have no idea what this Exception tells me. Maybe the problem has something to do with the properties being non standard? How are these properties even mapped to an easy table. Is a non-standard property like company simply split into several columns like Name, Street, City?
Or does the Exception point to some other problem with the local sync Store? As i said, the local syncstore is currently working fine and the pull from azure easy tables for some tables is also working fine.
I'm grateful for every answer that might point me in the right direction.
Per my understanding, you are handling 1:1 relationships between Contact and Company tables. For offline sync, you need to define the Contact and Company classes in your mobile client as follows:
For mobile client models
public class Contact : TableData
{
public string ContactName {get;set;}
public string Phone {get;set;}
public string Address {get;set;}
public string CompanyId {get;set;}
}
public class Company : TableData
{
public string CompanyName {get;set;}
public string Address {get;set;}
}
Note: As adrian hall's book Chapter3 :Relationships states about relationships on mobile client as follows:
There are a couple of rules you must follow within your client code:
You need to ensure that you create a Company before associating that Company with a Contact.
You need to store the CompanyId with the Contact, not the Company object (as you would normally do within Entity Framework).
If you InsertAsync a Company in offline mode, it will be placed into the operations queue prior to anything that uses it. Since the operations queue is processed in order, the Company will be sent to the backend prior to any record updates that would use it.
For Azure Easy Tables, you need to create both Contact and Company tables, and it would automatically create the Node.js backend for your created tables.
I would prefer handling tables individually and handling relationship management on the mobile client manually which could make the server much simpler by avoiding most of the complexity of relationships. Additionally, you could refer to Chapter 3 - Data Access and Offline Sync.

Accessing role ID through module

Im quite stuck here. In my cwebuser I've already defined my roles. My logins to my modules are restricted by roles, which is great! But my problem is restricting the modules to specific users within the roles. In webuser isShop is defined as a certain user id (user_role_id) in database to see if the user is user or shop. The issue is shop module can be seen by all roles who are isShop. My question is is there a way to authorize so that shop module gets user's id and shop id?
Something that mimics yii::app()->user->user_id;
like yii::app()->getmodule(shop)->shop_id;
Or must this be defined in model through criteria by shop_id? Doesn't sound right though, doing it this way.
I think if you are using the following function in model, you can apply the SHOP relations in here
public function defaultScope() {
if(isset(yii::app()->user->user_id)) return array('condition'=>'');
// here you can apply your conditions with the relation feilds
}

mongodb dbref java

I am newbie to mpngodb.
I have two collections.
Tenant
{
Tenant_ID:123, Tenant_Info:new
Tenant_ID:456, Tenant_Info:old
}
System
{
System_ID:768, Tenant_ID:123,System_Info:"check"
}
I need to reference the Tenant collection Tenant_ID with System collection Tenant_ID.
Could anyone help me with the DBREF java code for mongodb to achieve this relationship?
Database References (DBRefs) are used by convention; they do not correspond to a supported feature in the MongoDB server.
If you want to store a reference to another collection in your document you could do it a few ways:
1) Just save the relevant key for the other collection (in your example the Tenant_ID of the related Tenant document wants to be saved as a field in a document in the System collection).
2) Use the DBRef class to construct a reference to the object and then [fetch()](http://api.mongodb.org/java/current/com/mongodb/DBRef.html#fetch(com.mongodb.DB, com.mongodb.DBObject)) the referenced object.
Based on your example, it looks like the first usage would be more relevant - you appear to be saving the Tenant_ID field in your System document. In this case, you can load a System document and use findOne() to retrieve the related Tenant document based on the Tenant_ID (assuming Tenant_ID uniquely identifies a Tenant).
Take a look at Spring Data MongoDB.

MVC Get data from multiple tables in database for specific user

I'm new to MVC, currently running MVC 2, done a lot av regular ASP.Net in the past.
Background
Have a database containing all the tables for the .Net Membership provider.
Created a table called "Lists" containing UserID, ListID, ListName and so on.
It has a relationship key to the .Net Membership User, UserID column.
There is also a "List_Items" table that has ListID, ItemID, ItemName and so on.
What I have done
I managed to create a Entity Data Model mapping to the database. It contains both tables.
In the view I have done this:
<ul>
<% foreach (var item in Model) { %>
<li><%= Html.Encode(item.ListName) %></li>
<% } %>
</ul>
In the controller I have done this:
private ToDoDBEntities _db = new ToDoDBEntities();
return View(_db.Todo_Lists.ToList());
This I got from the asp.net/mvc tutorials. This tells me to things... I get all the Lists for all users. And I don't get the List Items from the other table.
What I want to do
I want to be able to pass the Provider Userkey to the database, for the logged on user, so I only get's his/hers lists.
I also would like to get Lists table and List Items table to the view in the same call so I can build the UL/IL with the List. And build content divs with the List Items.
Happy for any input.
/K
What tutorial are you using? Because I highly recommend http://www.asp.net/mvc as a learning resource, follow the tutorials and it will show you how to do what you want. It will also teach you techniques that will make life easier for you.

Getting user profile data in Silverlight 4

I am fairly new to Silverlight and RIA services, and I am trying to build a small project to understand it. So basically I created a Business Application, and I have the normal Login screen where I can add a user. That is fine and I can add a user and get him into the aspnet_Users table. Now I have created some extra fields, like Mob No, Tel No, DOB, Locality etc, which I have put in a table I have created called the UserProfile, and my idea is to get the username that was registered, and insert it into my UserProfile table with the other relevant data.
So I created a page called Profile.xaml, I created a UserProfileDomainService.cs where I have just one query, to get the user profile data from the table, and then created a Details DataGrid on my page, and the QueryName in my case is GetUserProfilesQuery(). Now what i wish to do is, get the user logged in, get his username, and check in my table to see if there is already data in the table. If there is populate the fields on the DataGrid with data, so that the user can modify this data, and if not, allow the user to insert data into the table.
So I have created a UserProfileViewModel class, and I want to create the query to get the data relevant to this user. However I got stuck on how to do all this, and how to get the user logged in.
Can anybody give me some advice or point me to some tutorials on how I can achieve this?
Thanks a lot and your help is very much appreciated.
In your domain service query you can use ServiceContext.User.Identity.Name to get the information specific to that user to include in your db query. I do something similar in our project.
We use entity framework so the LINQ to Entities query looks like:
return this.ObjectContext.UserSnapins
.Include("Snapin.EvolutionModule")
.Where(si => si.User.UserName == ServiceContext.User.Identity.Name)
.OrderBy(si => si.PageOrder);