Azure网站从移动应用程序生成不同的用户ID
问题描述:
以前,我将Azure移动应用程序用作我们的移动应用程序后端,现在我的公司要求我开发网站界面,并且所有当前用户都应该能够使用现有帐户登录。Azure网站从移动应用程序生成不同的用户ID
我用阿德里安方法,我的基地(link to guide)
一切工作正常。移动应用程序和网站使用相同的App服务。除网站生成验证时不同的用户ID进行比较,以移动应用(在我的情况的Facebook &谷歌)
网站用户
{蔚URL} /。认证/注册/ Facebook的
用户名:XXX
移动用户
使用LoginAsync()或致电{蔚URL}/auth /中登录/ Facebook的
用户名:SID:XXX
这是我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);
}
有什么我想念的吗?
答
根据你的描述,我猜你可能会误解帐号的用户ID和简单的auth端点用户ID。
据我所知,如果您在天蓝色的移动应用程序后端或天蓝色的网络应用程序中使用以下代码来获取用户ID,您会发现您获得相同的ID。
var claimsPrincipal = this.User as ClaimsPrincipal;
string sid = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier).Value;
该Id是每个用户的帐户ID。
您可以使用远程调试来获取ID,或者您可以访问{yourwebappname} .azurewebsites.com/.auth/me来获取ID。
我猜你的移动应用程序获得SID是象下面这样:
在我看来,SID是蔚蓝容易AUTH端点生成而不是用户帐户ID。
所以我建议你可以考虑使用用户帐户ID作为用户ID。
关于如何得到它,你可以发送请求到{yourwebappname}.azurewebsites.com/.auth/me
,它会返回用户信息json。
我的图片显示,我们可以在此json中获取用户帐户ID。