如何覆盖登录Url?
你能把它作为属性添加到AuthenticateAttribute中吗?
发布于 2012-08-09 18:01:03
在使用ServiceStacks的身份验证机制时,您从ServiceStackController或ServiceStackController<T>继承,后者又继承前者。
登录URL由ServiceStackController的LoginRedirectUrl属性指定。
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}"; }
}
}发布于 2015-03-24 17:17:25
不确定它是否是新的,但是看了代码,它实际上只是AuthFeature构造函数的第三个可选参数,所以你可以:
//htmlRedirect is optional 3rd param of AuthFeature constructor, here passing "~/signin"
Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new CredentialsAuthProvider(), }, "~/signin"));发布于 2012-07-03 06:04:14
在System.Web.Security.FormsAuthentication名称空间中:
FormsAuthentication.LoginUrl如果您想覆盖authorize值,只需实现您自己的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);
}
}
}https://stackoverflow.com/questions/11301790
复制相似问题