I created the request object from the class of my web service reference and assigned the enum value to the property of object.
SWS.QueueAccessLLSRQ.QueueAccessRQ request = new SWS.QueueAccessLLSRQ.QueueAccessRQ();
request.Version = "2.0.9";
request.Navigation = new SWS.QueueAccessLLSRQ.QueueAccessRQNavigation() { Action = SWS.QueueAccessLLSRQ.QueueAccessRQNavigationAction.I };
I'm expecting to have the XML request with attribute "Action" inside Navigation node like below:
<QueueAccessRQ xmlns="http://webservices.sabre.com/sabreXML/2011/10" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.0.9"> <Navigation Action="QR"/></QueueAccessRQ>
But after serialization I'm getting the next XML request without "Action" attribute.
<?xml version="1.0"?><QueueAccessRQ Version="2.0.9" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Navigation xmlns="http://webservices.sabre.com/sabreXML/2011/10"/></QueueAccessRQ>
Also, below are classes from web service reference that I'm using in my request:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2117.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://webservices.sabre.com/sabreXML/2011/10")]
public partial class QueueAccessRQ {
private QueueAccessRQNavigation navigationField;
private QueueAccessRQQueueIdentifier queueIdentifierField;
private QueueAccessRQSelection[] selectionField;
private bool returnHostCommandField;
private bool returnHostCommandFieldSpecified;
private System.DateTime timeStampField;
private bool timeStampFieldSpecified;
private string versionField;
public QueueAccessRQ() {
this.versionField = "2.0.9";
}
/// <remarks/>
public QueueAccessRQNavigation Navigation {
get {
return this.navigationField;
}
set {
this.navigationField = value;
}
}
/// <remarks/>
public QueueAccessRQQueueIdentifier QueueIdentifier {
get {
return this.queueIdentifierField;
}
set {
this.queueIdentifierField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Selection")]
public QueueAccessRQSelection[] Selection {
get {
return this.selectionField;
}
set {
this.selectionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool ReturnHostCommand {
get {
return this.returnHostCommandField;
}
set {
this.returnHostCommandField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool ReturnHostCommandSpecified {
get {
return this.returnHostCommandFieldSpecified;
}
set {
this.returnHostCommandFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime TimeStamp {
get {
return this.timeStampField;
}
set {
this.timeStampField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool TimeStampSpecified {
get {
return this.timeStampFieldSpecified;
}
set {
this.timeStampFieldSpecified = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Version {
get {
return this.versionField;
}
set {
this.versionField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute( "System.Xml", "4.7.2117.0" )]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true, Namespace = "http://webservices.sabre.com/sabreXML/2011/10" )]
public partial class QueueAccessRQNavigation {
private QueueAccessRQNavigationAction actionField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public QueueAccessRQNavigationAction Action
{
get
{
return this.actionField;
}
set
{
this.actionField = value;
}
}
}
Any idea how to not lost the attribute "Action" after serialization?
I will appreciate any help.
You are missing the attribute ActionSpecified, I have added a snippet of the payload construction.
QueueAccessRQ request = new QueueAccessRQ()
{
Version = "2.0.9",
Navigation = new QueueAccessRQNavigation()
{
Action = QueueAccessRQNavigationAction.I,
ActionSpecified = true
}
};
Related
In my ViewModel i want o load the Picker source RegionName data from an Azure Region table. I extract data from table in an async method but the Picker displays an empty List even after ObservableCollection or List has changed or crashes.
When using PropertyChanged on the ListRegion list itself the app crashes.
In my models:
public class Region
{
public string Id { get; set; }
public string RegionName { get; set; }
}
In my RegisterPage.xaml:
<Picker SelectedIndex="{Binding RegionsSelectedIndex, Mode=TwoWay}"
ItemsSource="{Binding Regions}"
Margin="0,15,0,0"
Title="Select a region">
</Picker>
in my RegisterPage.cs:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class RegisterPage : ContentPage
{
RegisterViewModel registerVM;
public RegisterPage ()
{
InitializeComponent ();
registerVM = new RegisterViewModel();
this.BindingContext = registerVM;
}
protected override void OnAppearing()
{
base.OnAppearing();
}
in my RegisterPageViewModel:
public class RegisterViewModel: INotifyPropertyChanged
{
ApiServices _apiServices = new ApiServices();
public RegisterViewModel()
{
initializePickerAsync();
}
async private void initializePickerAsync()
{
try
{
var regionsList = await App.MobileService.GetTable<Models.Region>().ToListAsync();
List<string> regionsStringList = new List<string>();
foreach (Models.Region reg in regionsList)
{
regionsStringList.Add(reg.RegionName);
}
Regions = regionsStringList;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/*
private RegisterViewModel (ObservableCollection<Models.Region> regionData)
{
ObservableCollection<string> regionDataAsStringList = new ObservableCollection<string>();
foreach (Models.Region r in regionData)
{
regionDataAsStringList.Add(r.RegionName);
}
this.Regions = regionDataAsStringList;
}
async public static Task<RegisterViewModel> BuildViewModelAsync()
{
ObservableCollection<Models.Region> tmpRegionData = new ObservableCollection<Models.Region>(await App.MobileService.GetTable<Models.Region>().ToListAsync());
return new RegisterViewModel(tmpRegionData);
}
*/
int regionsSelectedIndex = 0;
public int RegionsSelectedIndex
{
get
{
return regionsSelectedIndex;
}
set
{
if (regionsSelectedIndex != value)
{
regionsSelectedIndex = value;
OnPropertyChanged(nameof(RegionsSelectedIndex));
if (regionsSelectedIndex >= 0)
{
Region = Regions[regionsSelectedIndex];
}
}
}
}
// public ObservableCollection<Region> Regions {get;set}
public List<string> Regions
{
get
{
return Regions;
}
set
{
if (Regions != value)
{
Regions = value;
OnPropertyChanged(nameof(Regions));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
you seem to be doing a lot of unnecessary creating and assigning of different lists of data. You should be able to create your ObservableCollection ONCE and then add your data to it, something like this
in your ViewModel
ObservableCollection<Region> Regions { get; set; }
public RegisterViewModel()
{
Regions = new ObservableCollection<Region>();
}
public async void GetData()
{
var regionsList = await App.MobileService.GetTable<Models.Region>().ToListAsync();
foreach (Models.Region reg in regionsList)
{
Regions.Add(reg);
}
}
in your page
protected async override void OnAppearing()
{
base.OnAppearing();
await registerVM.GetData();
}
New to Xamarin development! Using Visual Studio 2017 and all the latest installs and updates for my dev environment.
I have my app "shell" working in that it will run, navigate, crud to local db, and sync to rest services. So, the base of my app is sound. I am trying to integrate the ZXing barcode scanner into my app. Most of what help I can find relates to raw forms or code behind xaml. I am using views and view models and I don't know how to translate the information I am finding to this model.
I currently have the xaml view showing the "camera viewer" when a button is clicked so I can see a barcode using the camera. However, there is no "red line" in the camera view and subsequently, there are no events being fired to let me know what is going on. I really am confused as I am so new to Xamarin. I don't know where to start.
XAML View page
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials"
xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="Views.InventoryPage1"
Title="Inventory Page">
<StackLayout Spacing="5" Padding="10,10,10,0">
<!--<fe:BindablePicker ItemsSource="{Binding Areas}" SelectedItem="{Binding SelectedArea}" DisplayProperty="AreaName" Title="Select Your Area" />
<fe:BindablePicker ItemsSource="{Binding Reasons}" SelectedItem="{Binding SelectedReason}"
DisplayProperty="Description" Title="Select a Reason" />
<Label Text="Scan"/>
<Entry Text="{Binding Barcode}"/>
<Label Text="Quantity"/>
<Entry Text="{Binding Quantity}"/>
<Button Text="Save" Style="{StaticResource Button_Primary}" Command="{Binding SaveCommand}" />-->
<forms:ZXingScannerView WidthRequest="100" HeightRequest="100" IsScanning="{Binding IsScanning}" IsAnalyzing="{Binding IsAnalyzing}" Result="{Binding Result, Mode=TwoWay}" ScanResultCommand="{Binding QRScanResultCommand}" ></forms:ZXingScannerView>
</StackLayout>
</ContentPage>
ViewModel
public class InventoryPage1ViewModel : BindableBase, INavigationAware
{
private readonly IPageDialogService _pageDialogService;
private bool _isAnalyzing = true;
private bool _isScanning = true;
public ZXing.Result Result { get; set; }
public List<Area> Areas { get; private set; }
public Area SelectedArea { get; set; }
public List<Reason> Reasons { get; private set; }
public Reason SelectedReason { get; set; }
public int Quantity { get; set; }
public string Barcode { get; set; }
public DelegateCommand SaveCommand => new DelegateCommand(PerformSave);
public DelegateCommand QRScanResultCommand => new DelegateCommand(QRCommand);
private readonly IAreaService _areaService;
private readonly IScanService _scanService;
private readonly IReasonService _reasonService;
public InventoryPage1ViewModel(IAreaService areaService, IScanService scanService, IReasonService reasonService, IPageDialogService pageDialogService)
{
_pageDialogService = pageDialogService;
_reasonService = reasonService;
_scanService = scanService;
_areaService = areaService;
Areas = _areaService.GetAll();
Reasons = _reasonService.GetAll();
}
public bool IsScanning
{
get
{
return _isScanning;
}
set
{
_isScanning = value;
RaisePropertyChanged();
}
}
public bool IsAnalyzing
{
get
{
return _isAnalyzing;
}
set
{
_isAnalyzing = value;
RaisePropertyChanged();
}
}
private void QRCommand()
{
int x = 1;
}
private async void PerformSave()
{
var scan = new Scan()
{
AreaId = SelectedArea.Id,
InsertDateTime = DateTime.Now,
ReasonId = SelectedReason.Id,
ScanItem = Barcode,
ScanQty = Quantity,
IsUploaded = false
};
// Save it to the DB here.
var retVal = _scanService.Insert(scan);
if (retVal)
{
await _pageDialogService.DisplayAlertAsync("Saved", "Scan saved successfully.", "OK");
}
else
{
// TODO: Inform the user something went wrong.
}
}
int _index;
public int SelectIndex
{
get
{
return _index;
}
set
{
_index = value;
RaisePropertyChanged("SelectIndex");
}
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public void OnNavigatedTo(NavigationParameters parameters)
{
}
public void OnNavigatingTo(NavigationParameters parameters)
{
}
}
MainActivity
public class MainActivity :
global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.tabs;
ToolbarResource = Resource.Layout.toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
LoadApplication(new App(new AndroidInitializer()));
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IConnectionFactory, ConnectionFactory>();
}
}
UPDATE
I have a working view model now thanks to #Krzysztof over at Xamarin.Forms.
New ViewModel
public class InventoryPage1ViewModel : BindableBase, INavigationAware
{
private readonly IPageDialogService _pageDialogService;
public List<Area> Areas { get; private set; }
public Area SelectedArea { get; set; }
public List<Reason> Reasons { get; private set; }
public Reason SelectedReason { get; set; }
public int Quantity { get; set; }
public DelegateCommand SaveCommand => new DelegateCommand(PerformSave);
private readonly IAreaService _areaService;
private readonly IScanService _scanService;
private readonly IReasonService _reasonService;
public InventoryPage1ViewModel(IAreaService areaService, IScanService scanService, IReasonService reasonService, IPageDialogService pageDialogService)
{
_pageDialogService = pageDialogService;
_reasonService = reasonService;
_scanService = scanService;
_areaService = areaService;
Areas = _areaService.GetAll();
Reasons = _reasonService.GetAll();
}
public ZXing.Result Result { get; set; }
private string barcode = string.Empty;
public string Barcode
{
get
{
return barcode;
}
set
{
barcode = value;
RaisePropertyChanged();
}
}
private bool isAnalyzing = true;
public bool IsAnalyzing
{
get { return this.isAnalyzing; }
set
{
if (!bool.Equals(this.isAnalyzing, value))
{
this.isAnalyzing = value;
RaisePropertyChanged(nameof(IsAnalyzing));
}
}
}
private bool isScanning = true;
public bool IsScanning
{
get { return this.isScanning; }
set
{
if (!bool.Equals(this.isScanning, value))
{
this.isScanning = value;
RaisePropertyChanged(nameof(IsScanning));
}
}
}
public Command QRScanResultCommand
{
get
{
return new Command(() =>
{
IsAnalyzing = false;
IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
Barcode = Result.Text;
await _pageDialogService.DisplayAlertAsync("Scanned Item", Result.Text, "Ok");
});
IsAnalyzing = true;
IsScanning = true;
});
}
}
private async void PerformSave()
{
var scan = new Scan()
{
AreaId = SelectedArea.Id,
InsertDateTime = DateTime.Now,
ReasonId = SelectedReason.Id,
ScanItem = Barcode,
ScanQty = Quantity,
IsUploaded = false
};
// Save it to the DB here.
var retVal = _scanService.Insert(scan);
if (retVal)
{
await _pageDialogService.DisplayAlertAsync("Saved", "Scan saved successfully.", "OK");
}
else
{
// TODO: Inform the user something went wrong.
}
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public void OnNavigatedTo(NavigationParameters parameters)
{
}
public void OnNavigatingTo(NavigationParameters parameters)
{
}
}
I have the below function and am getting an error on the second line saying Unspecified value parameters :: type[T], Type mismatch, expected: Command[NotInferedT], actual: DeletePrivilegeMappingCmd.
execute() takes in a parameter of Command[T] and DeletePrivilegeMappingCmd extends that Command[T] requirement.
I figured out based on the error I need to add a [Object] to the signature, but I'm not sure what Type I have to include inside the []. Adding any Object gets rid of the error, but I'm assuming there will be a runtime error if the correct type is not used.
What type am I supposed to add?
Error message signature
def deleteGroupPrivilegeMapping(privilegeId: String, groupId: String) {
commandExecutor.execute(new DeletePrivilegeMappingCmd(privilegeId, null, groupId))
}
Updated message signature
def deleteGroupPrivilegeMapping(privilegeId: String, groupId: String) {
commandExecutor.execute(new DeletePrivilegeMappingCmd(privilegeId, null, groupId)[String][String])
}
EDIT:
Signature for Command and execute
public interface Command<T> extends BaseCommand<T, CommandContext> {
T execute(CommandContext commandContext);
}
Implementation for DeletePrivilegeMappingCmd
public class DeletePrivilegeCmd implements Command<Void>, Serializable {
private static final long serialVersionUID = 1L;
protected String id;
public DeletePrivilegeCmd(String id) {
if (id == null) {
throw new FlowableIllegalArgumentException("id is null");
}
this.id = id;
}
public Void execute(CommandContext commandContext) {
commandContext.gePrivilegeMappingEntityManager().deleteByPrivilegeId(id);
commandContext.getPrivilegeEntityManager().delete(id);
return null;
}
}
Command Context Definition
public class CommandContext extends AbstractCommandContext {
private static Logger log = LoggerFactory.getLogger(CommandContext.class);
protected ProcessEngineConfigurationImpl processEngineConfiguration;
protected FailedJobCommandFactory failedJobCommandFactory;
protected FlowableEngineAgenda agenda;
protected Map<String, ExecutionEntity> involvedExecutions = new HashMap(1);
protected LinkedList<Object> resultStack = new LinkedList();
public CommandContext(Command<?> command, ProcessEngineConfigurationImpl processEngineConfiguration) {
super(command);
this.processEngineConfiguration = processEngineConfiguration;
this.failedJobCommandFactory = processEngineConfiguration.getFailedJobCommandFactory();
this.sessionFactories = processEngineConfiguration.getSessionFactories();
this.agenda = processEngineConfiguration.getAgendaFactory().createAgenda(this);
}
protected void logException() {
if(!(this.exception instanceof JobNotFoundException) && !(this.exception instanceof FlowableTaskAlreadyClaimedException)) {
super.logException();
} else {
log.info("Error while closing command context", this.exception);
}
}
public void addCloseListener(CommandContextCloseListener commandContextCloseListener) {
if(this.closeListeners == null) {
this.closeListeners = new ArrayList(1);
}
this.closeListeners.add(commandContextCloseListener);
}
public DbSqlSession getDbSqlSession() {
return (DbSqlSession)this.getSession(DbSqlSession.class);
}
public EntityCache getEntityCache() {
return (EntityCache)this.getSession(EntityCache.class);
}
public DeploymentEntityManager getDeploymentEntityManager() {
return this.processEngineConfiguration.getDeploymentEntityManager();
}
public ResourceEntityManager getResourceEntityManager() {
return this.processEngineConfiguration.getResourceEntityManager();
}
public ByteArrayEntityManager getByteArrayEntityManager() {
return this.processEngineConfiguration.getByteArrayEntityManager();
}
public ProcessDefinitionEntityManager getProcessDefinitionEntityManager() {
return this.processEngineConfiguration.getProcessDefinitionEntityManager();
}
public ModelEntityManager getModelEntityManager() {
return this.processEngineConfiguration.getModelEntityManager();
}
public ProcessDefinitionInfoEntityManager getProcessDefinitionInfoEntityManager() {
return this.processEngineConfiguration.getProcessDefinitionInfoEntityManager();
}
public ExecutionEntityManager getExecutionEntityManager() {
return this.processEngineConfiguration.getExecutionEntityManager();
}
public TaskEntityManager getTaskEntityManager() {
return this.processEngineConfiguration.getTaskEntityManager();
}
public IdentityLinkEntityManager getIdentityLinkEntityManager() {
return this.processEngineConfiguration.getIdentityLinkEntityManager();
}
public VariableInstanceEntityManager getVariableInstanceEntityManager() {
return this.processEngineConfiguration.getVariableInstanceEntityManager();
}
public HistoricProcessInstanceEntityManager getHistoricProcessInstanceEntityManager() {
return this.processEngineConfiguration.getHistoricProcessInstanceEntityManager();
}
public HistoricDetailEntityManager getHistoricDetailEntityManager() {
return this.processEngineConfiguration.getHistoricDetailEntityManager();
}
public HistoricVariableInstanceEntityManager getHistoricVariableInstanceEntityManager() {
return this.processEngineConfiguration.getHistoricVariableInstanceEntityManager();
}
public HistoricActivityInstanceEntityManager getHistoricActivityInstanceEntityManager() {
return this.processEngineConfiguration.getHistoricActivityInstanceEntityManager();
}
public HistoricTaskInstanceEntityManager getHistoricTaskInstanceEntityManager() {
return this.processEngineConfiguration.getHistoricTaskInstanceEntityManager();
}
public HistoricIdentityLinkEntityManager getHistoricIdentityLinkEntityManager() {
return this.processEngineConfiguration.getHistoricIdentityLinkEntityManager();
}
public EventLogEntryEntityManager getEventLogEntryEntityManager() {
return this.processEngineConfiguration.getEventLogEntryEntityManager();
}
public JobEntityManager getJobEntityManager() {
return this.processEngineConfiguration.getJobEntityManager();
}
public TimerJobEntityManager getTimerJobEntityManager() {
return this.processEngineConfiguration.getTimerJobEntityManager();
}
public SuspendedJobEntityManager getSuspendedJobEntityManager() {
return this.processEngineConfiguration.getSuspendedJobEntityManager();
}
public DeadLetterJobEntityManager getDeadLetterJobEntityManager() {
return this.processEngineConfiguration.getDeadLetterJobEntityManager();
}
public AttachmentEntityManager getAttachmentEntityManager() {
return this.processEngineConfiguration.getAttachmentEntityManager();
}
public TableDataManager getTableDataManager() {
return this.processEngineConfiguration.getTableDataManager();
}
public CommentEntityManager getCommentEntityManager() {
return this.processEngineConfiguration.getCommentEntityManager();
}
public PropertyEntityManager getPropertyEntityManager() {
return this.processEngineConfiguration.getPropertyEntityManager();
}
public EventSubscriptionEntityManager getEventSubscriptionEntityManager() {
return this.processEngineConfiguration.getEventSubscriptionEntityManager();
}
public HistoryManager getHistoryManager() {
return this.processEngineConfiguration.getHistoryManager();
}
public JobManager getJobManager() {
return this.processEngineConfiguration.getJobManager();
}
public void addInvolvedExecution(ExecutionEntity executionEntity) {
if(executionEntity.getId() != null) {
this.involvedExecutions.put(executionEntity.getId(), executionEntity);
}
}
public boolean hasInvolvedExecutions() {
return this.involvedExecutions.size() > 0;
}
public Collection<ExecutionEntity> getInvolvedExecutions() {
return this.involvedExecutions.values();
}
public FailedJobCommandFactory getFailedJobCommandFactory() {
return this.failedJobCommandFactory;
}
public ProcessEngineConfigurationImpl getProcessEngineConfiguration() {
return this.processEngineConfiguration;
}
public FlowableEventDispatcher getEventDispatcher() {
return this.processEngineConfiguration.getEventDispatcher();
}
public FlowableEngineAgenda getAgenda() {
return this.agenda;
}
public Object getResult() {
return this.resultStack.pollLast();
}
public void setResult(Object result) {
this.resultStack.add(result);
}
}
I tried bind property of custom type in View Model with custom View, but in Binder method "SetValue(object value)" value is always null. Why it happens? Is it impossible bind property of custom type with custom view in MvvmCross?
My ViewModel:
public class RecipesFiltersVM : MvxViewModel
{
public SearchField DishField { get; private set; }
public SearchField CuisineField { get; private set; }
public SearchField IngredientField { get; private set; }
private readonly IFiltersService _filtersService;
public RecipesFiltersVM(IFiltersService filtersService)
{
_filtersService = filtersService;
UpdateSearchFields ();
}
private async void UpdateSearchFields ()
{
var allFilters = await _filtersService.LoadAllFilters ();
DishField = new SearchField (
allFilters
.Where(f => f.Type == FilterType.Dish)
.ToList()
);
CuisineField = new SearchField (
allFilters
.Where(f => f.Type == FilterType.Cuisine)
.ToList()
);
IngredientField = new SearchField (
allFilters
.Where(f => f.Type == FilterType.Ingredient)
.ToList()
);
}
}
My SearchField:
public class SearchField : MvxViewModel
{
private String _searchResult;
public String SearchResult {
get { return _searchResult; }
set {
_searchResult = value;
RaisePropertyChanged (() => SearchResult);
UpdateFoundFilters ();
}
}
private ObservableCollection<Filter> _foundFilters;
public ObservableCollection<Filter> FoundFilters {
get { return _foundFilters; }
set {
_foundFilters = value;
RaisePropertyChanged (() => FoundFilters);
}
}
}
In CustomView:
public class SearchFieldView : UIView
{
public UITextField SearchResult { get { return _searchResult; } }
private UITextField _searchResult;
public UITableView FoundFilters { get { return _foundFilters; } }
private UITableView _foundFilters;
}
In Binder:
public class SearchFieldViewWithSearchFieldBinder : MvxTargetBinding
{
protected SearchFieldView SearchFieldView {
get { return (SearchFieldView)Target; }
}
public SearchFieldViewWithSearchFieldBinder (SearchFieldView target)
: base (target)
{
}
public override void SetValue (object value)
{
//value is always null!
}
public override Type TargetType {
get
{
return typeof(SearchField);
}
}
public override MvxBindingMode DefaultMode {
get
{
return MvxBindingMode.TwoWay;
}
}
}
Setup:
protected override void FillTargetFactories (Cirrious.MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterCustomBindingFactory<SearchFieldView> (
"SearchField",
indicators => new SearchFieldViewWithSearchFieldBinder(indicators)
);
base.FillTargetFactories (registry);
}
In ViewController:
var set = this.CreateBindingSet<RecipesFiltersDialog, RecipesFiltersVM>();
set.Bind (_dish).For("SearchField").To (vm => vm.DishField);
set.Bind (_cuisine).For("SearchField").To (vm => vm.CuisineField);
set.Bind (_ingredient).For("SearchField").To (vm => vm.IngredientField);
set.Apply ();
UPD
Solved with two ways update ViewModel code. First I changed custom property declaration like:
private SearchField _dishField;
public SearchField DishField {
get
{
return _dishField;
}
set
{
_dishField = value;
RaisePropertyChanged (() => DishField);
}
}
Second I initialize my properties in ViewModel constructor before UpdateSearchFields () execution:
public RecipesFiltersVM(IFiltersService filtersService)
{
_filtersService = filtersService;
DishField = new SearchField (new List<Filter> ());
CuisineField = new SearchField (new List<Filter> ());
IngredientField = new SearchField (new List<Filter> ());
UpdateSearchFields ();
}
You need to create your own custom bindings for your custom view. For instance if you have followed the normal MvvmCross practice of using public C# properties and C# events to define the items in your custom view then you should be able to do something like this:
public class SearchFieldView : UIView, IMvxBindable
{
public IMvxBindingContext BindingContext { get; set; }
public SearchFieldView()
{
this.CreateBindingContext();
}
public UITextField SearchResult { get { return _searchResult; } }
private UITextField _searchResult;
public UITableView FoundFilters { get { return _foundFilters; } }
private UITableView _foundFilters;
[MvxSetToNullAfterBinding]
public object DataContext
{
get { return BindingContext.DataContext; }
set { BindingContext.DataContext = value; }
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
BindingContext.ClearAllBindings();
}
base.Dispose(disposing);
}
}
And then it should just "work." For inspiration checkout things like MvxView or MvxTableViewCell.
Alternatively you may be able to do your own MvxPropertyInfoTargetBinding like how MvvmCross is handling the UISegmentedControlBindings by implementing something like MvxUISegmentedControlSelectedSegmentTargetBinding.
Solved with Stuart's comment in two ways update ViewModel code. First I changed custom property declaration like:
private SearchField _dishField;
public SearchField DishField {
get
{
return _dishField;
}
set
{
_dishField = value;
RaisePropertyChanged (() => DishField);
}
}
Second I initialize my properties in ViewModel constructor before UpdateSearchFields () execution:
public RecipesFiltersVM(IFiltersService filtersService)
{
_filtersService = filtersService;
DishField = new SearchField (new List<Filter> ());
CuisineField = new SearchField (new List<Filter> ());
IngredientField = new SearchField (new List<Filter> ());
UpdateSearchFields ();
}
I've got an MVC 2 application which won't be doing its own authentication, but will retrieve a user ID from the HTTP request header, since users must pass through a gateway before reaching the application.
Once in the app, we need to match up the user ID to information in a "users" table, which contains some security details the application makes use of.
I'm familiar with setting up custom membership and roles providers in ASP.NET, but this feels so different, since the user never should see a login page once past the gateway application.
Questions:
How do I persist the user ID, if at all? It starts out in the request header, but do I have to put it in a cookie? How about SessionState?
Where/when do I get this information? The master page shows the user's name, so it should be available everywhere.
I'd like to still use the [Authorize(Roles="...")] tag in my controller if possible.
We have a very similar setup where I work. As #Mystere Man mentioned, there are risks with this setup, but if the whole infrastructure is setup and running correctly, we have found it to be a secure setup (we do care about security). One thing to ensure, is that the SiteMinder agent is running on the IIS node you're trying to secure, as it will validate an encrypted SMSESSION key also passed in the headers, which will make the requests secure (it would be extremely difficult to spoof the value of the SMSESSION header).
We are using ASP.NET MVC3, which has global action filters, which is what we're using. But with MVC2, you could create a normal, controller level action filter that could be applied to a base controller class so that all of your controllers/actions will be secured.
We have created a custom configuration section that allows us to turn this security filter on and off via web.config. If it's turned off, our configuration section has properties that will allow you to "impersonate" a given user with given roles for testing and debugging purposes. This configuration section also allows us to store the values of the header keys we're looking for in config as well, in case the vendor ever changes the header key names on us.
public class SiteMinderConfiguration : ConfigurationSection
{
[ConfigurationProperty("enabled", IsRequired = true)]
public bool Enabled
{
get { return (bool)this["enabled"]; }
set { this["enabled"] = value; }
}
[ConfigurationProperty("redirectTo", IsRequired = true)]
public RedirectToElement RedirectTo
{
get { return (RedirectToElement)this["redirectTo"]; }
set { this["redirectTo"] = value; }
}
[ConfigurationProperty("sessionCookieName", IsRequired = true)]
public SiteMinderSessionCookieNameElement SessionCookieName
{
get { return (SiteMinderSessionCookieNameElement)this["sessionCookieName"]; }
set { this["sessionCookieName"] = value; }
}
[ConfigurationProperty("userKey", IsRequired = true)]
public UserKeyElement UserKey
{
get { return (UserKeyElement)this["userKey"]; }
set { this["userKey"] = value; }
}
[ConfigurationProperty("rolesKey", IsRequired = true)]
public RolesKeyElement RolesKey
{
get { return (RolesKeyElement)this["rolesKey"]; }
set { this["rolesKey"] = value; }
}
[ConfigurationProperty("firstNameKey", IsRequired = true)]
public FirstNameKeyElement FirstNameKey
{
get { return (FirstNameKeyElement)this["firstNameKey"]; }
set { this["firstNameKey"] = value; }
}
[ConfigurationProperty("lastNameKey", IsRequired = true)]
public LastNameKeyElement LastNameKey
{
get { return (LastNameKeyElement)this["lastNameKey"]; }
set { this["lastNameKey"] = value; }
}
[ConfigurationProperty("impersonate", IsRequired = false)]
public ImpersonateElement Impersonate
{
get { return (ImpersonateElement)this["impersonate"]; }
set { this["impersonate"] = value; }
}
}
public class SiteMinderSessionCookieNameElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
public class RedirectToElement : ConfigurationElement
{
[ConfigurationProperty("loginUrl", IsRequired = false)]
public string LoginUrl
{
get { return (string)this["loginUrl"]; }
set { this["loginUrl"] = value; }
}
}
public class UserKeyElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
public class RolesKeyElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
public class FirstNameKeyElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
public class LastNameKeyElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
public class ImpersonateElement : ConfigurationElement
{
[ConfigurationProperty("username", IsRequired = false)]
public UsernameElement Username
{
get { return (UsernameElement)this["username"]; }
set { this["username"] = value; }
}
[ConfigurationProperty("roles", IsRequired = false)]
public RolesElement Roles
{
get { return (RolesElement)this["roles"]; }
set { this["roles"] = value; }
}
}
public class UsernameElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
public class RolesElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
So our web.config looks something like this
<configuration>
<configSections>
<section name="siteMinderSecurity" type="MyApp.Web.Security.SiteMinderConfiguration, MyApp.Web" />
...
</configSections>
...
<siteMinderSecurity enabled="false">
<redirectTo loginUrl="https://example.com/login/?ReturnURL={0}"/>
<sessionCookieName value="SMSESSION"/>
<userKey value="SM_USER"/>
<rolesKey value="SN-AD-GROUPS"/>
<firstNameKey value="SN-AD-FIRST-NAME"/>
<lastNameKey value="SN-AD-LAST-NAME"/>
<impersonate>
<username value="ImpersonateMe" />
<roles value="Role1, Role2, Role3" />
</impersonate>
</siteMinderSecurity>
...
</configuration>
We have a custom SiteMinderIdentity...
public class SiteMinderIdentity : GenericIdentity, IIdentity
{
public SiteMinderIdentity(string name, string type) : base(name, type) { }
public IList<string> Roles { get; set; }
}
And a custom SiteMinderPrincipal...
public class SiteMinderPrincipal : GenericPrincipal, IPrincipal
{
public SiteMinderPrincipal(IIdentity identity) : base(identity, null) { }
public SiteMinderPrincipal(IIdentity identity, string[] roles) : base(identity, roles) { }
}
And we populate HttpContext.Current.User and Thread.CurrentPrincipal with an instance of SiteMinderPrincipal that we build up based on information that we pull from the request headers in our action filter...
public class SiteMinderSecurity : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
var request = filterContext.HttpContext.Request;
var response = filterContext.HttpContext.Response;
if (MyApp.SiteMinderConfig.Enabled)
{
string[] userRoles = null; // default to null
userRoles = Array.ConvertAll(request.Headers[MyApp.SiteMinderConfig.RolesKey.Value].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), r => r.Trim());
var identity = new SiteMinderIdentity(request.Headers[MyApp.SiteMinderConfig.UserKey.Value];, "SiteMinder");
if (userRoles != null)
identity.Roles = userRoles.ToList();
var principal = new SiteMinderPrincipal(identity, userRoles);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = principal;
}
else
{
var roles = Array.ConvertAll(MyApp.SiteMinderConfig.Impersonate.Roles.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), r => r.Trim());
var identity = new SiteMinderIdentity(MyApp.SiteMinderConfig.Impersonate.Username.Value, "SiteMinder") { Roles = roles.ToList() };
var principal = new SiteMinderPrincipal(identity, roles);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = principal;
}
}
}
MyApp is a static class that gets initialized at application startup that caches the configuration information so we're not reading it from web.config on every request...
public static class MyApp
{
private static bool _isInitialized;
private static object _lock;
static MyApp()
{
_lock = new object();
}
private static void Initialize()
{
if (!_isInitialized)
{
lock (_lock)
{
if (!_isInitialized)
{
// Initialize application version number
_version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
_siteMinderConfig = (SiteMinderConfiguration)ConfigurationManager.GetSection("siteMinderSecurity");
_isInitialized = true;
}
}
}
}
private static string _version;
public static string Version
{
get
{
Initialize();
return _version;
}
}
private static SiteMinderConfiguration _siteMinderConfig;
public static SiteMinderConfiguration SiteMinderConfig
{
get
{
Initialize();
return _siteMinderConfig;
}
}
}
From what I gather of your situation, you have information in a database that you'll need to lookup based on the information in the headers to get everything you need, so this won't be exactly what you need, but it seems like it should at least get you started.
Hope this helps.