1

如何覆盖登录网址?

你能把它作为属性添加到 AuthenticateAttribute 中吗?

4

3 回答 3

4

不确定它是否是新的,但查看了代码,它实际上只是 AuthFeature 构造函数的可选第三个参数,所以你可以:

//htmlRedirect is optional 3rd param of AuthFeature constructor, here passing "~/signin"
Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new CredentialsAuthProvider(), }, "~/signin"));
于 2015-03-24T09:17:25.060 回答
2

使用 ServiceStacks 的身份验证机制时,您可以从其中一个继承,ServiceStackController或者ServiceStackController<T>后者又继承前者。

登录 URL 由 的LoginRedirectUrl属性决定ServiceStackController

public virtual string LoginRedirectUrl
{
    get { return "/login?redirect={0}"; }
}

由于它是虚拟的,您可以简单地在您自己的控制器中覆盖它。或者更好的是,创建自己的抽象基础控制器,继承自ServiceStackController. 然后让你所有的控制器继承那个。您现在有一个点可以控制登录 URL 等内容。

public abstract class MyControllerBase : ServiceStackController
{
    public override string LoginRedirectUrl
    {
        get { return "/letslogin?redirectTo={0}"; }
    }
}
于 2012-08-09T10:01:03.357 回答
1

在 System.Web.Security.FormsAuthentication 命名空间中:

FormsAuthentication.LoginUrl

如果要覆盖 Web.config 值,只需实现自己的授权属性:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CustomAuthorize: AuthorizeAttribute{
    public override void OnAuthorization(AuthorizationContext filterContext) {
        //If the request does not provide authentication, then perform a redirect
        if (!filterContext.HttpContext.Request.IsAuthenticated) {
            var loginUrl = FormsAuthentication.LoginUrl; //Change your URL here if needed.

            filterContext.Result = new RedirectResult(loginUrl);
        } else {
            //Since the request was authenticated, perform the default authorization check.
            base.OnAuthorization(filterContext);
        }
    }
}
于 2012-07-02T22:04:13.570 回答