ServiceStack剃刀认证 [英] ServiceStack Razor Authentication

查看:109
本文介绍了ServiceStack剃刀认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看的Rockstars的例子,ServiceStack.Razor。

I am looking at the Rockstars example and ServiceStack.Razor.

我如何去装修认证成,也就是说,secure.cshtml页。所以,我可以在需要用户重定向到Login.cshtml。

How do I go about fitting authentication into, say, secure.cshtml page. So I can redirect user to Login.cshtml if required.

我只从SocialBootstrapApi例如明白,如果我混MVC HYBIRD,我可以把[验证()]在ServiceStackController来实现这一目标。

I only understand from SocialBootstrapApi example if I mix MVC hybird, I can put [authenticate()] at ServiceStackController to achieve that.

但如果我只是想不.NET MVC纯SS项目?

But what if I just want a pure SS project without .net MVC?

推荐答案

的<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.ServiceInterface/AuthenticateAttribute.cs\">Authenticate属性是只是一个普通ServiceStack 请求过滤属性的,也就是说,它工作在两个MVC和ServiceStack。

The Authenticate attribute is just a plain ServiceStack Request Filter Attribute, i.e. it works in both MVC and ServiceStack.

应用此过滤器会返回一个 401未授权作为所有非HTML请求的响应。例如如果你叫这个使用Ajax,可以检测到这种错误响应并执行客户端上的重定向。

Applying this filter will return a 401 UnAuthorized response for all non-HTML requests. e.g. If you called this with Ajax, you could detect this error response and do the redirect on the client.

从ServiceStack的v3.9.23 +的 [授权] 属性将自动重定向所有验证错误,〜/登录默认网址。

From v3.9.23+ of ServiceStack the [Authenticate] attribute will automatically redirect all Authentication errors to ~/login url by default.

当您注册AuthFeature,您可以覆盖这个网址例如:

You can override this url when you register the AuthFeature, e.g:

Plugins.Add(new AuthFeature(...) { HtmlRedirect = "/path/to/my/login" });

这将在全球范围适用于所有 [授权] 属性,也可以覆盖此即席基础上:

Which will apply globally to all [Authenticate] attributes or you can override this on an adhoc basis with:

[Authenticate(HtmlRedirect="/path/to/my/login")]

请注意:属性是可继承的,所以你可以一次添加给 SecuredService 类和所有子类将继承其行为

Note: Attributes are inheritable so you can add this once to a SecuredService class and all subclasses will inherit its behaviour.

要重定向未经授权的HTML请求,以手动,你可以做你自己检查+重定向与

To redirect an UnAuthorized HTML request manually you can do your own checking + redirection with:

public object Secured(Request request) {
    if (!base.SessionAs<MyCustomSession>().IsAuthenticated)
        return new HttpResult(HttpStatusCode.Redirect, "Un Authorized") { 
           Headers = { {"Location", "/path/to/login" } } };
}

还有围绕上述重定向干燥包装,你可以改用:

There is also a DRY wrapper around the above redirect which you can use instead:

public object Secured(Request request) {
    if (!base.SessionAs<MyCustomSession>().IsAuthenticated)
        return HttpResult.Redirect("/path/to/login");
}

这篇关于ServiceStack剃刀认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆