Redirect to ReturnUrl after successful cookie authentication in Owin, Katana & Nancy

独自空忆成欢 提交于 2019-12-13 11:43:52

问题


I am using Owin, Katana and Nancy to host a simple site with an authentication required section. Note I am also using nuget package - Nancy.MSOwinSecurity

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = Constants.AuthenticationType,
    LoginPath = new PathString("/Login"),
});
app.UseNancy();

Here is my module code

public class LoginModule : NancyModule
{
    public LoginModule()
    {
        Post["login"] = p =>
        {
            var name = Request.Form.name;
            var auth = Context.GetAuthenticationManager();
            var claims = new List<Claim> {new Claim(ClaimTypes.Name, name)};
            var id = new ClaimsIdentity(claims, Constants.AuthenticationType);
            auth.SignIn(id);
            // redirect how????
            return View["index"];
        };
    }
}

My submitting form

<form name="login" action="/login" method="post" accept-charset="utf-8">
    <ul>
        ...
    </ul>
</form>

Now I am looking to redirect after successful login to the ReturnUrl -

e.g. Login?ReturnUrl=%2Fblah%2blahblah

There seems to be no redirect method like in forms authentication plus the query string parameters property is empty.


回答1:


Have you tried Response.AsRedirect("/"); or GetRedirect("/"); on NancyContext

Using your code sample:

public class LoginModule : NancyModule
{
    public LoginModule()
    {
        Post["login"] = p =>
        {
            var name = Request.Form.name;
            var auth = Context.GetAuthenticationManager();
            var claims = new List<Claim> {new Claim(ClaimTypes.Name, name)};
            var id = new ClaimsIdentity(claims, Constants.AuthenticationType);

            auth.SignIn(id);

            return Response.AsRedirect(p.Request.Query.RedirectUrl);
        };
     }
}


来源:https://stackoverflow.com/questions/23539987/redirect-to-returnurl-after-successful-cookie-authentication-in-owin-katana-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!