简体   繁体   English

Azure网站从移动应用程序生成不同的用户ID

[英]Azure Website generate different userID from Mobile App

Previously I'm using Azure Mobile App as our Mobile Application backend and now my company ask me to develop Website interface and all our current user should be able to login with existing account. 以前,我使用Azure移动应用程序作为我们的移动应用程序后端,现在我的公司要求我开发网站界面,我们目前的所有用户都应该能够使用现有帐户登录。

I'm using Adrian method as my base ( link to guide ) 我使用Adrian方法作为基础( 指向指南的链接

Everything work fine. 一切正常。 The Mobile App and Website is under the same App service. 移动应用程序和网站在同一应用程序服务下。 Except the website generate different userID compare to the mobile app when authenticated (in my case Facebook & Google) 除了网站通过身份验证后与移动应用程序生成的用户ID不同(在我的情况下为Facebook和Google)

Website User 网站用户

{azure-URL}/.auth/login/facebook {azure-URL} /。auth / login / facebook

UserID : xxx 用户名:xxx

<div class="panel-body">          
  <a href="/.auth/login/facebook?post_login_redirect_url=/Home" class="btn btn-block btn-social btn-facebook">
    <i class="fa fa-facebook"></i> Sign in with Facebook
  </a>              
</div>

Mobile User 移动用户

Using LoginAsync() Or calling {azure-URL}/.auth/login/facebook 使用LoginAsync()或调用{azure-URL} /。auth / login / facebook

UserID : sid:xxx 用户名:sid:xxx

This is my Startup.cs 这是我的Startup.cs

    public static void ConfigureMobileApp(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        new MobileAppConfiguration()
            .AddTables(
                new MobileAppTableConfiguration()
                    .MapTableControllers()
                    .AddEntityFramework())
            .MapApiControllers()
            .AddPushNotifications()
            .ApplyTo(config);

        // Use Entity Framework Code First to create database tables based on your DbContext
        // Database.SetInitializer(new MobileServiceInitializer());
        var migrator = new DbMigrator(new Migrations.Configuration());
        migrator.Update();

        MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();


        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new   CookieAuthenticationProvider
            {
                OnApplyRedirect = ctx =>
                {
                    if (!IsZumoAuth(ctx.Request))
                    {

                        ctx.Response.Redirect(ctx.RedirectUri);
                    }
                }
            }

        });



        if (string.IsNullOrEmpty(settings.HostName))
         {

             app.Use(typeof(CustomAppServiceAuthenticationMiddleware), app, new AppServiceAuthenticationOptions
             //app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
             {
                 // This middleware is intended to be used locally for debugging. By default, HostName will
                 // only have a value when running in an App Service application.
                 SigningKey = ConfigurationManager.AppSettings["SigningKey"],
                 ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
                 ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
                 TokenHandler = config.GetAppServiceTokenHandler()
             });
         }



        app.UseWebApi(config);
    }

Is there anything I miss? 我有什么想念的吗?

According to your description, I guess you may misunderstand the account user id and easy auth endpoint user id. 根据您的描述,我想您可能会误解帐户用户ID和简单身份验证端点用户ID。

As far as I know, if you use below codes in azure mobile app backend or azure web app to get the user id.You will find you get the same id. 据我所知,如果您在azure移动应用程序后端或azure网络应用程序中使用以下代码来获取用户ID,则会发现您获得了相同的ID。

var claimsPrincipal = this.User as ClaimsPrincipal;
string sid = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier).Value;

The Id is each user's account id. 该ID是每个用户的帐户ID。

You could use remote debug to get the id or you could access {yourwebappname}.azurewebsites.com/.auth/me to get the id. 您可以使用远程调试获取ID,也可以访问{yourwebappname} .azurewebsites.com / .auth / me获取ID。

在此处输入图片说明

I guess your mobile app get sid is like below: 我猜您的移动应用程序的sid如下所示:

在此处输入图片说明

In my opinion, the sid is the azure easy auth endpoint generate not the user account id. 我认为sid是azure easy auth端点生成的,而不是用户帐户ID。

So I suggest you could consider using the user account id as the user id. 因此,我建议您可以考虑使用用户帐户ID作为用户ID。

About how to get it, you could send the request to the {yourwebappname}.azurewebsites.com/.auth/me , it will return the user information json. 关于获取方法,您可以将请求发送到{yourwebappname}.azurewebsites.com/.auth/me ,它将返回用户信息json。

We could get the user account id in this json as my image shows. 如我的图像所示,我们可以在此json中获取用户帐户ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM