【问题标题】:ASP.NET Boilerplate - Authorization not workingASP.NET 样板 - 授权不起作用
【发布时间】:2015-11-01 07:54:15
【问题描述】:

我正在使用 ASP.NET Boilerplate 框架创建一个具有授权但无法正常工作的应用程序。

ajax 方法正在调用 Login 方法并检索执行成功功能的正确数据并确认:“登录!”。 (只有登录信息正确,否则报错)。

我希望“AuthenticationManager.SignIn”会处理所有登录功能(似乎是零模块中的情况)。但是在登录并转到应用了 [AbpMvcAuthorize] 的控制器后,我最终会出现一个页面,通知我我没有打开该页面的权限。

Javascript:

(function () {
    $('#LoginButton').click(function (e) {
        e.preventDefault();
        abp.ui.setBusy(
            $('#LoginArea'),
            abp.ajax({
                url: abp.appPath + 'Account/Login',
                type: 'POST',
                    data: JSON.stringify({
                    usernameOrEmailAddress: $('#EmailAddressInput').val(),
                    password: $('#PasswordInput').val(),
                    rememberMe: $('#RememberMeInput').is(':checked')
                }),
                success: function (data) {
                    if (data != null) {
                        confirm(data);
                   }
                },
                error: function () {
                    confirm("Something went wrong. Try again later!");
                }
            })
        );
    });
})();

帐户控制器:

[HttpPost]
public async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "")
{
    try
    {
        if (!ModelState.IsValid)
        {
            throw new UserFriendlyException("Your form is invalid!");
        }

        var loginResult = await _userManager.LoginAsync(
            loginModel.UsernameOrEmailAddress,
            loginModel.Password,
            loginModel.TenancyName
        );

        switch (loginResult.Result)
        {
            case AbpLoginResultType.Success:
                break;
            case AbpLoginResultType.InvalidUserNameOrEmailAddress:
                ...
        }

        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
        AuthenticationManager.SignIn(new AuthenticationProperties { 
            IsPersistent = loginModel.RememberMe }, loginResult.Identity);

        if (string.IsNullOrWhiteSpace(returnUrl))
        {
            returnUrl = Request.ApplicationPath;
        }
    }
    catch (UserFriendlyException ex)
    {
        return Json(ex.Message);
    }
    return Json("Logged in!");
}

编辑:在AuthenticationManager.Sign(..)之后,我可以通过loginResult.User看到登录的用户信息。所以我猜登录有效,但 [AbpMvcAuthorize] 有问题?

【问题讨论】:

    标签: c# jquery asp.net asp.net-mvc-5 asp.net-boilerplate


    【解决方案1】:

    这很奇怪。您是如何创建解决方案的?来自模板 (http://www.aspnetboilerplate.com/Templates)? 因为,它在模板中正常工作(见模板的HomeController:https://github.com/aspnetboilerplate/module-zero-template/blob/master/src/AbpCompanyName.AbpProjectName.WebSpaAngular/Controllers/HomeController.cs) 那么,你能与之相比吗?

    【讨论】:

    • 我是从 ABP 模板制作的,然后使用 nuget 添加了模块零包。我已经比较了两者很长时间了,仍然找不到问题。我可以假设“AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = loginModel.RememberMe }, loginResult.Identity);”完全照顾所有必要的登录功能?
    • 在AuthenticationManager.Sign(..)之后,通过loginResult.User可以看到登录的用户信息。所以我猜登录有效,但 [AbpMvcAuthorize] 有问题?
    • 我从一个干净的模板开始,其中包含模块零并且它有效。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多