How to Set DataProtectorTokenProvider TokenLifetime in VB (Not c#) - owin

Looked at this question which seems to address my need.
I need the VB.net (2015 preferred) syntax for this snippet. Specifically, Assign the TokenLifespan line.
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"))
{
// Added custom code to set a different lifespan
TokenLifespan = TimeSpan.FromHours(3)
};
}

Well, I have looked a day and never did find an answer to my C# to VB syntax question. No 'freely available online converter' was able handle the snippet. I tried perhaps a dozen or more. The ones that didn't error, skipped the property assignment.
In the end, I managed to code around the unknown syntax by instantiating an object and then assigning the property.
Here is the code I used to set the DataProtectorTokenProvider.TokenLifespan value:
If dataProtectionProvider IsNot Nothing Then
Dim oTokenProvider As Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider(Of ApplicationUser)
oTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser)(dataProtectionProvider.Create("ASP.NET Identity"))
oTokenProvider.TokenLifespan = TimeSpan.FromMinutes(15)
manager.UserTokenProvider = oTokenProvider
End If
Perhaps this will help someone else down the road.
:-)

Related

Sorting a Listbox in C++

first time asking questions here. I'm having trouble sorting a listbox in a c++ form. I would much rather do this in C# but it's a class requirement unfortunately. Here's the code in question:
void updateCourseDisplay()
{
courseListBox->Items->Clear();
set<Course*> courseSet = courseControl->getCourseSet();
std::set<Course *>::iterator it;
for (it = courseSet.begin(); it != courseSet.end(); ++it)
{
Course * c = (*it);
String ^courseId = gcnew String((c->getID().c_str()));
String ^courseName = gcnew String((c->getName().c_str()));
courseListBox->Items->Add(courseId + " - " + courseName);
}
courseListBox->System::Windows::Forms::ListBox::Sort();
populateCopyStudentsComboBox();
}
I know this is pretty messy but I didn't want to mess around with creating managed objects to add to the form. The error I'm getting when I try and run this says that System::Windows::Forms::ListBox::Sort() would have been called but it is inaccessible. All I want is simple ordering of the list by the string value. Any thoughts?
Sort is a protected method that can not be accessed from the "outside". You should set the public Sorted property to true instead.

Why does my Groovy AST transform insert null at the end of my method?

I have written an AST transform that creates a setter for a JPA mapped property (it both sets the local field and calls setOwner on the other end of the relationship):
private static void createSetter(FieldNode field) {
Parameter parameter = GeneralUtils.param(field.getType(), field.getName());
BlockStatement body = new BlockStatement();
body.addStatement(assignS(fieldX(field), varX(parameter)));
MethodCallExpression setterCall = callX(varX(parameter), "setOwner", varX("this", field.getDeclaringClass()));
setterCall.setType(ClassHelper.VOID_TYPE);
body.addStatement(stmt(setterCall));
MethodNode method = new MethodNode(setterName(field.getName()), ACC_PUBLIC, ClassHelper.VOID_TYPE, new Parameter[] {parameter}, ClassNode.EMPTY_ARRAY, body);
field.getDeclaringClass().addMethod(method);
}
This works, but the generated method has a strange null statement at the end as disassembled by JD-GUI (in addition to an odd local variable):
public void setMore(Simple_MoreStuff more) {
Simple_MoreStuff localSimple_MoreStuff = more;
this.more = localSimple_MoreStuff;
more.setOwner(this);
null;
}
It doesn't seem to affect the actual correctness, but it's odd, and it seems like a bug. In MethodCallExpression, I found this comment but don't know if it relates, since my method is in fact void (I explicitly set it above, and it makes no difference):
//TODO: set correct type here
// if setting type and a methodcall is the last expression in a method,
// then the method will return null if the method itself is not void too!
// (in bytecode after call: aconst_null, areturn)
Is there a way to keep the generated method from having the spurious null?
I have not looked at JD-GUI, so I cannot tell how capable this tool is in understanding bytecode, that does not come from Java. But in general disassemblers can only somewhat show what Java code in that case might look like, by no means it is supposed to show correct code from a non-Java language. So better do not expect correct Java code if you disassemble Groovy.
In this case I suspect that JD-GUI stumbles over a workaround we have not gotten rid of yet. In several cases we add at the method end dead code, the const_null, areturn you have noticed. We do this because of problems with the verifier if a bytecode label is used at the end of a method. And since the dead code does not influence correctness we are currently using this solution.

Why does my variable say it is unassigned when it is assigned?

I'm am helping convert VB code to C#. In the C# code, I have an error saying that one of my variables is unassigned. When I right click (in Visual Studios 2013) and click on Go to Definition it brings me to its declaration where it is clearly being assigned to null. I have even tried assigning it to something else besides null.
Here is the declaration and it being set to null:
DataSet set5 = new DataSet();
set5 = null;
Here is where it is being called and where I get the error:
try
{
wires.grdInser.DataSource = set5.Tables[0];//Being called here
} catch (Exception exception92)
{
ProjectData.SetProjectError(exception92);
Exception exception46 = exception92;
ProjectData.ClearProjectError();
}
There is a bunch of code in-between these two pieces of code, but I don't think the scope the variable is being called in is an issue since the "Go to Definition" takes me directly to the definition. Could the issue be because it is inside a try/catch statement? Any help would be greatly appreciated.
Try this instead, just to get rid of the error, then you can figure out what you need to do from there:
DataSet set5 = new DataSet();
DataTable x = new DataTable();
set5.Tables.Add(x);
Get rid of the set5 = null statement.
Now your other function should be able to see Tables[0].

Using late binding to Excel, how do I move from the active cell to the first cell in the next row?

I am using late binding (in C#, using Reflection, etc.) to access Excel via COM. In other words, I start by getting the Excel.Application object using
Type excelType = Type.GetTypeFromProgId("Excel.Application");
object excelApplication = Activator.CreateInstance(excelType);
(Actually, it's more generic than that, but that gives the idea).
As many people have noted in StackOverflow, one good way to see how to use the COM interfaces to Office is to record a macro in Excel and then look at the VBA code to see which class members to invoke.
When I do that, I see that if you have a reference to the active cell (obtained from the ActiveCell property of the Excel.Application object), then VBA references ActiveCell.Offset(x,y) to reference a cell relative to that ActiveCell (x and y being the row and column indices). However, if I try to access the Offset method of a Cell with late binding, the InvokeMember method fails with an exception saying "Member not found".
If I use the Visual Studio Object Browser to inspect the Microsoft.Office.Interop.Excel namespace and the ApplicationClass class (which I assume gives a good look into the COM interface obtained by the above C# code getting it via the ProgId), it shows that ApplicationClass has an ActiveCell property which is of type Microsoft.Office.Interop.Excel.Range. And inspecting that class shows that indeed it does NOT have a member named "Offset", hence the "Member not found" when I try to Invoke it.
That seems to mean that the VBA macros are using a different object model for Excel from that which is exposed via COM! Is that right, or am I missing something? And if it is the case, how can I get at that same VBA object model via late binding in COM?
Or, is there some way, using methods other than Offset, to be able to move the ActiveCell to the beginning of the next row, which is really what I want?
You could try this:
dim i as integer
i = ActiveCell.Row + 1
Excel.ActiveSheet.Cells(i,1).value = "Whatever your value is."
The answer is, as usual, easy when you know how.
My code a bit further down from the sample in my question looked a bit like this:
object activeCell;
object result;
// in here code to get the correct value into activeCell
if(activeCell.GetType().GetProperty("Offset") == null){
result = activeCell.GetType().InvokeMember("Offset", BindingFlags.InvokeMethod, null, activeCell, new object[] {1, 0});
}
It was that InvokeMember that threw an exception with the message "Member not found".
When I changed the parameters to InvokeMember to read as follows:
result = activeCell.GetType().InvokeMember("Offset", BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, activeCell, new object[] {1, 0});
then it worked.
Just why it works is still a mystery, which I am sure will earn someone some reputation if they answer it.

How to add a Calculated field to AllContentType?

Today, I'm having a problem is after I had created a Calculated field. It seems there is no way to add AllContentTypes. And the DefaultView, maybe I can handle this. And I also saw this method:
spList.Fields.AddFieldAsXml(spFieldUser.SchemaXml, True, SPAddFieldOptions.AddToAllContentTypes);
But in this case, I'm not sure I can use it or not. Because my code is:
//SPField tempSPField = spList.Fields.CreateNewField(createSPColumnObject.ColumnType, createSPColumnObject.ColumnName);//We can not use this code line for creating Calculated (there is no constructor for this)
SPFieldCollection collFields = spList.Fields;
string strSPFieldCalculatedName = collFields.Add(createSPColumnObject.ColumnName, SPFieldType.Calculated, false);
if (createSPColumnObject.IsAddedToDefaultView)
{
SPView spView = spList.DefaultView;
spView.ViewFields.Add(strSPFieldCalculatedName);
spView.Update();
}
SPFieldCalculated spFieldCalculated = null;
//
spFieldCalculated = (SPFieldCalculated)collFields[createSPColumnObject.ColumnName];
spFieldCalculated.ShowInDisplayForm = true;
//spFieldCalculated.ShowInEditForm = true;
spFieldCalculated.ShowInListSettings = true;
//spFieldCalculated.ShowInNewForm = true;
spFieldCalculated.ShowInViewForms = true;
//
spFieldCalculated.Description = createSPColumnObject.ColumnDescription;
spFieldCalculated.Formula = string.Format(#"={0}",createSPColumnObject.CalcFormula);
spFieldCalculated.Update();
//spList.Fields.AddFieldAsXml(spFieldCalculated.SchemaXml, createSPColumnObject.IsAddedToDefaultView, SPAddFieldOptions.AddToAllContentTypes);// also use this code line because we will get an exception with a duplicate column ID.
spFieldCalculated.OutputType = SPFieldType.Text;
spList.Update();
I totally created a Calculated column but how can I add it to allcontent types ? everybody could help me out this ? BTW, to the DefaultView, I did like the above is right ? Could eveybody let me know this ?
I just worry about everybody get misunderstanding ? Or review with missing code. So could everybody please to take a look on my code clearly ? Thanks all.
Many thanks, :)
Standley Nguyen
I'm not sure if i fully understand what you are trying to do however i may be able to shed some light on some parts of what you are trying to do.
When you create your field does it then appear in your site actions -> site settings -> Site columns. If so you have created this correctly. If it doesn't there are hundreds of examples of how to do this if you search google.
Once you have your field create you then need to consider which content types you want to add it to. Once you have these content types you then have to add something called a field link to the Content type.
This isn't my code i have picked it off the web but this should do what you require.
SPContentType ct = web.ContentTypes[contentType];
ct.FieldLinks.Add(new SPFieldLink(field));
ct.Update();
Cheers
Truez

Resources