Saturday, November 7, 2009

WCF REST HttpStatusCode.Unauthorized status code does not work

Alternate title: WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized does not work.
Alternate title: HttpStatusCode.Unauthorized turned into HttpStatusCode.NotFound

The problem:
I spent at least three hours tracking this stupid thing down. The problem that I ran into was Forms Authentication. What's happening is that I am returning HttpStatusCode.Unauthorized (401), but forms authentication is routing the error to the login page. If you don't have a Login.aspx, you will get a resource not found error (HttpStatusCode.NotFound - 404).

If you can change your authentication mode to Windows and the problem goes away, you have this problem.
OR
If you use the browser to hit your REST web service that requires authentication (it returns a 401 status code) and it routes you to your aspx login screen, you have this problem.

Possible solutions:
  • Check out this MSDN article: Supporting HTTP Authentication and Forms Authentication in a Single ASP.NET Web Site . If for some reason this link dies, search for Mixed Authentication Disposition ASP.NET Module (MADAM), which the name of the HttpModule that the MSDN article talks about that allows you to use both HTTP authentication (in this case basic/digest) and Forms authentication in the same web site.
  • I did run across a couple of post were people wanted to use both Windows and Forms authentication together. In those post, it was suggested that the people create a virtual directory in IIS so that you could have two different web.config that specify different authentication methods. I don't know if this will work for this problem, but it is a possible alternative.





Thursday, September 10, 2009

Remote Desktop Drive sharing not working

I ran into a Windows 2003 server on our local network today that would not allow me to share drives so that I could upload/download files to/from the server.

My remote desktop settings look like this:


However, when I connected and opened up windows explorer, my local drives did not appear in the tree view on the left.

Update ->  There two ways to fix this.

Way 1:
After a little bit of digging, I discovered that the group policy for drive sharing (a.k.a. "Drive redirection") was not configured on the Windows 2003 server. This can be fixed by using the MMC snap in called "Group Policy Object Editor":


After navigating to "\Local Computer Policy\Computer Configuration\Administrative Templates\Windows Components\Terminal Services\Client/Server data redirect", I changed the "Do not allow drive redirection" from "Not Configured" to "Disabled" under and it worked fine afterward:




Way 2:
This can also be accomplished this way on Windows Server 2003:



Steps from the Administrative tools window under the control panel.
  1. Double click on "Terminal Service Configuration"
  2. Left click "Connections" on the dialog
  3. Right click "Rdp-Tcp" in the right hand windows.
  4. Uncheck "Drive mapping" on the "Client Settings" tab.

Thursday, July 9, 2009

CurrentSessionContext call results in "No current session context configured."

Well, I was playing with NHibernate today and trying to create a single session in a windows application that would stay open till I was ready to close it. I was using CurrentSessionContext.Bind(someSession) when I generated this error: "No current session context configured." After some searching around, I discovered that I was missing a line in my Hibernate.cfg.xml file.

< name="current_session_context_class" > thread_static < /property >

Apparently, it can have several settings:
  • managed_web
  • call
  • thread_static
  • web

References:

Thursday, June 25, 2009

RichTextBox with better spacing

It took a bit of looking around today to figure out how to create a resource file entry to style my RichTextBox so that it doesn't have ridiculous spacing. I found this, which shows how to do it inside the page or window. However, I wanted to put it in a resource file so that I could reuse it. I finally came up with this:


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="RichTextWithBetterSpacing" TargetType="{x:Type RichTextBox}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontFamily" Value="Arial"/>
<Style.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</Style.Resources>
</Style>
</ResourceDictionary>

Wednesday, December 10, 2008

Custom RoleProvider notes

I finally completed my custom RoleProvider that uses NHibernate to retrieve data from the database. Below I have listed my notes on this topic.

Issue: My RoleProvider would not work if I overrode the default constructor.
Solution: Override the Initialize method and put your code there instead.

Issue: The RoleProvider class cannot access HttpContext.Current.Session.
Solution: There really isn't a solution. It's just a fact.

Resources:

Custom MembershipProvider notes

I finally completed my custom MembershipProvider that uses NHibernate to retrieve data from the database. Below I have listed my notes on this topic.

Issue: I could not get the "Web Site Administration Tool" to create a user without giving me an error.

Solution: Make sure that the following property returns true:
public override bool RequiresQuestionAndAnswer
If it was false, I would get a generic error that made absolutely no sense:

The following message may help in diagnosing the problem: Exception has been thrown by the target of an invocation. at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Web.Administration.WebAdminMembershipProvider.CallWebAdminMembershipProviderHelperMethodOutParams(String methodName, Object[] parameters, Type[] paramTypes) at System.Web.Administration.WebAdminMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status) at System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() at System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) at System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.WebControls.Wizard.WizardChildTable.OnBubbleEvent(Object source, EventArgs args) at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Issue: I ran into a couple of examples that verify the password by unencoding the one in the database and then comparing it to the user's entry.

Solution: I recommend that you do it the same way that Microsoft did it. That is to encode the user's entry and compare it to the encoded database entry. It eliminates the need to have an unencode method.


Other things worth noting:

If you are going to use Encryption encoding (as opposed to clear text or hash encoding), don't forget to generate your own MachineKey. Here are a few link on that topic:

Resources:

Thursday, December 4, 2008

WCF & the IIS Require secure channel (ssl) setting

After turning on "Require secure channel (SSL)" on the IIS 6.0 Directory security tab (secure communications dialog), I lost several hours today trying to figure out why my WCF services stopped working and started giving me this error:
[System.ServiceModel.ServiceActivationException] = {"The requested service, 'https://www.Dave.com/DoIt/MyService.svc' could not be activated. See the server's diagnostic trace logs for more information."}

I tried a variety of things, but when I finally broke down and ran the diagnostic trace I got the following:
The service '/DoIt/MyService.svc' cannot be activated due to an exception during compilation. The exception message is: Could not find a base address that matches scheme http for the endpoint with binding MetadataExchangeHttpBinding. Registered base address schemes are [https]..

All my services use https to send data; however, my metadata exchange binding, which I intended to remove/comment out later, use http. When I changed all my metadata exchanged end points to use mexHttpsBinding instead of mexHttpBinding and all my behaviors to use <servicemetadata httpsgetenabled="true"> instead of <servicemetadata httpgetenabled="true">, everything started working again.

I figured that the http endpoints would just be ignored or if accessed permissioned denied because all addresses must use a secure channel (https). It appears that I was wrong and that it causes a compilation error.