【发布时间】:2017-03-09 00:16:36
【问题描述】:
我正在开发一个带有 .NET 后端的 Xamarin Forms 移动应用程序。我关注this guide 并成功设置了自定义身份验证,并在 Startup.cs 中进行了一次更改:
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
{
SigningKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY"),
ValidAudiences = new[] { Identifiers.Environment.ApiUrl },
ValidIssuers = new[] { Identifiers.Environment.ApiUrl },
TokenHandler = config.GetAppServiceTokenHandler()
});
没有“if (string.IsNullOrEmpty(settings.HostName))”。否则,我总是在登录后对所有请求都未经授权。
服务器项目:
-
授权控制器
公共类 ClubrAuthController : ApiController { 私有只读 ClubrContext dbContext; 私有只读 ILoggerService loggerService;
public ClubrAuthController(ILoggerService loggerService) { this.loggerService = loggerService; dbContext = new ClubrContext(); } public async Task<IHttpActionResult> Post(LoginRequest loginRequest) { var user = await dbContext.Users.FirstOrDefaultAsync(x => x.Email == loginRequest.username); if (user == null) { user = await CreateUser(loginRequest); } var token = GetAuthenticationTokenForUser(user.Email); return Ok(new { authenticationToken = token.RawData, user = new { userId = loginRequest.username } }); } private JwtSecurityToken GetAuthenticationTokenForUser(string userEmail) { var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, userEmail) }; var secretKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY"); var audience = Identifiers.Environment.ApiUrl; var issuer = Identifiers.Environment.ApiUrl; var token = AppServiceLoginHandler.CreateToken( claims, secretKey, audience, issuer, TimeSpan.FromHours(24) ); return token; }}
-
Startup.cs
ConfigureMobileAppAuth(app, config, container); app.UseWebApi(config); } private void ConfigureMobileAppAuth(IAppBuilder app, HttpConfiguration config, IContainer container) { config.Routes.MapHttpRoute("ClubrAuth", ".auth/login/ClubrAuth", new { controller = "ClubrAuth" }); app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions { SigningKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY"), ValidAudiences = new[] { Identifiers.Environment.ApiUrl }, ValidIssuers = new[] { Identifiers.Environment.ApiUrl }, TokenHandler = config.GetAppServiceTokenHandler() }); }
客户项目:
MobileServiceUser user = await MobileClient.LoginAsync(loginProvider, jtoken);
此外,我在 azure 门户中配置了 Facebook 提供程序,如 here 所述。 但只有当我注释掉 app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions(){...});在 Startup.cs 中。 让两种类型的身份验证同时工作我缺少什么?
【问题讨论】:
标签: authentication xamarin.forms azure-mobile-services