servicestack,Asp.net Mvc,Authentication,servicestack" /> servicestack,Asp.net Mvc,Authentication,servicestack" />

Asp.net mvc 如何覆盖ServiceStack.MVC身份验证中的登录Url?

Asp.net mvc 如何覆盖ServiceStack.MVC身份验证中的登录Url?,asp.net-mvc,authentication,servicestack,Asp.net Mvc,Authentication,servicestack,如何覆盖登录Url 能否将其添加到System.Web.Security.FormsAuthentication命名空间中的AuthenticateAttribute作为属性?: FormsAuthentication.LoginUrl 如果要覆盖Web.config值,只需实现自己的authorize属性: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inher

如何覆盖登录Url


能否将其添加到System.Web.Security.FormsAuthentication命名空间中的AuthenticateAttribute作为属性?

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

[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);
        }
    }
}

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

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

[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);
        }
    }
}

使用ServiceStacks的身份验证机制时,您可以从
ServiceStackController
ServiceStackController
继承,后者反过来继承前者

登录URL由
ServiceStackController
LoginDirectURL
属性指定

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}"; }
    }
}

使用ServiceStacks的身份验证机制时,您可以从
ServiceStackController
ServiceStackController
继承,后者反过来继承前者

登录URL由
ServiceStackController
LoginDirectURL
属性指定

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}"; }
    }
}

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

//htmlRedirect is optional 3rd param of AuthFeature constructor, here passing "~/signin"
Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new CredentialsAuthProvider(), }, "~/signin"));

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

//htmlRedirect is optional 3rd param of AuthFeature constructor, here passing "~/signin"
Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new CredentialsAuthProvider(), }, "~/signin"));

我的问题是针对不同的身份验证机制。ServiceStack.ServiceInterface.AuthenticateAttribute我的问题是针对不同的身份验证机制。ServiceStack.ServiceInterface.AuthenticateAttribute