如何使Servicestack身份验证在Umbraco安装中工作

时间:2012-10-09 14:23:33

标签: umbraco servicestack

我无法将SS身份验证与Umbraco安装一起使用。每当我使用Authenticate属性访问DTO或服务时,我都会被重定向到umbraco登录。 重现: 我创建了一个新项目,并从Nuget安装了Umbraco(尝试了4.7.1,4.8.1和4.9.0,结果相同),并从Nuget安装了SS。我将SS设置为在web.config中的/ api路径下运行:

  <location path="api">
    <system.web>
      <httpHandlers>
        <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
      </httpHandlers>
    </system.web>
    <!-- Required for IIS 7.0 -->
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
      </handlers>
    </system.webServer>
  </location>

并将/ api添加到保留路径以避免Umbraco处理此问题:

<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/api" />

在AppHost.cs中,我更改了EndpointHostConfig并启用了身份验证:

   SetConfig(new EndpointHostConfig
   {
       DebugMode = true, //Show StackTraces when developing
       ServiceStackHandlerFactoryPath = "api"
   });

//Enable Authentication
ConfigureAuth(container);

我已经更改了ConfigureAuth中的连接字符串以使用Umbraco数据库:

    var connectionString = ConfigurationManager.AppSettings["umbracoDbDSN"];

最后我在HelloWorldService上设置了属性:

[Authenticate]
public class HelloService : ServiceBase<Hello>
{
    protected override object Run(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}

然后我通过REST Uri访问HelloWorldService:

http://localhost:56835/api/hello

马上我被重定向到:

http://localhost:56835/apilogin?redirect=http%3a%2f%2flocalhost%3a56835%2fapi%2fhello

并显示默认的Umbraco 404页面,这意味着SS突然让Umbraco处理身份验证。如果我删除Authenticate属性,SS会完美地处理请求。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

看起来FormsAuthentication只是简单烦人,当它发现401时,它会改变为302 login.aspx。 我的解决方案是使用此链接中的想法:http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx

简而言之,它将FormsAuthentication的401-> 302更改还原为401。 我在检测代码中稍微改了一下:

 if (response.StatusCode == 401 && request.RawUrl.ToLower().StartsWith("/api"))

并删除了Register并在web.config

中的FormsAuthentication模块之后手动添加它