【问题标题】:Azure Mobile Services custom authentication with MVC5使用 MVC5 的 Azure 移动服务自定义身份验证
【发布时间】:2015-10-06 17:44:53
【问题描述】:

我已经使用 Azure 移动服务和我的 Windows Phone 8.1 应用成功设置了自定义身份验证过程(按照指南 here

我现在正在创建一个 MVC5 单页应用程序 (SPA) 来管理系统的管理方面。我对 MVC5 比较陌生,只需要一点帮助就可以开始执行登录,就像在我的手机应用中一样?

目前我的手机应用通过以下方式登录

App.MobileService.CurrentUser = await AuthenticateAsync(this.textBox_email.Text, textBox_password.Password);

会的

    private async Task<MobileServiceUser> AuthenticateAsync(string username, string password)
    {

        // Call the CustomLogin API and set the returned MobileServiceUser
        // as the current user.
        var user = await App.MobileService
            .InvokeApiAsync<LoginRequest, MobileServiceUser>(
            "CustomLogin", new LoginRequest()
            {
                UserName = username,
                Password = password
            });

        return user;
    }

这一切都很好,所以我想问题是我如何在 MVC5 中以相同的方式调用我的客户身份验证 API 并在成功时设置用户上下文?

Startup.Auth.cs:

 public partial class Startup
{
           public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Uncomment the following lines to enable logging in with third party login providers
        //app.UseMicrosoftAccountAuthentication(
        //    clientId: "",
        //    clientSecret: "");

        //app.UseTwitterAuthentication(
        //   consumerKey: "",
        //   consumerSecret: "");

        //app.UseFacebookAuthentication(
        //   appId: "",
        //   appSecret: "");

        //app.UseGoogleAuthentication();
    }

如果我缺少任何信息或细节,请告诉我。 谢谢!

【问题讨论】:

    标签: asp.net-mvc-5 azure-mobile-services


    【解决方案1】:

    不幸的是,这在移动服务中并不容易做到。虽然您可以使用移动服务 HTML/JS SDK(在 MVC 视图中提供)实现登录,但这不会设置用户上下文。

    由于移动服务与 MVC 不兼容(在新的 Mobile Apps 产品中解决),您将无法依赖该 SDK。不幸的是,这意味着编写自定义中间件/过滤器。

    最简单的解决方案可能是将您的用户名/密码验证和存储逻辑打包到您的移动服务项目和 MVC 项目可以共享的代码中。 MVC 项目需要获取经过验证的用户并发出会话 cookie,然后由自定义中间件或过滤器读取。

    编写AuthorizationFilter 实现将比 OWIN 中间件容易得多,因此我会推荐这种方法。检查 cookie 是否存在且有效,如果存在则设置用户上下文。

    【讨论】:

    • 谢谢你 :) 会试一试。我最初开始使用移动应用程序,但由于移动应用程序尚不支持自定义身份验证,因此恢复到 Azure 移动服务 - 这仍然是真的吗?
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多