I see that I can add Nancy to my Azure Mobile app. http://www.strathweb.com/2014/02/running-owin-pipeline-new-net-azure-mobile-services/ , but how to add authentication for Nancy? The goal here is to have both a web application and a mobile application using the same server.
Purpose . If the Nancy page requires authentication, go to ~ / .auth / login / aad (for example), and then return to the original page.
Where I am:
- New Azure App for ASP.NET Mobile Applications Created
- Added by Nancy
Delete default MobileAppConfig and replace with
new MobileAppConfiguration()
.MapApiControllers()
.AddTables(
new MobileAppTableConfiguration()
.MapTableControllers()
.AddEntityFramework()
)
.AddPushNotifications()
.MapLegacyCrossDomainController()
.ApplyTo(config);
Create an IndexModule and Confirm Nancy Works
public IndexModule()
{
Get["/"] = _ => "Hello";
}
Create an AdminModule, RequiresAuthentication will appear after installing Nancy.Authentication.Forms
public AdminModule()
: base("admin")
{
Get["/"] = _ =>
{
this.RequiresAuthentication();
return "This is admin";
};
}
Maybe wrong, but I
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
base.ConfigureRequestContainer(container, context);
container.Register<IUserMapper, UserMapper>();
}
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
base.RequestStartup(container, pipelines, context);
var formsAuthConfig = new FormsAuthenticationConfiguration
{
RedirectUrl = "~/.auth/login/aad",
UserMapper = container.Resolve<IUserMapper>(),
};
FormsAuthentication.Enable(pipelines, formsAuthConfig);
}
This means, and (3) still does not work.Update. Nancy.Forms.Authentication seems to be causing this. Can I use UseCookieAuthentication
from Owin.Security?
Update2. I got rid of Nancy.Forms.Authentication. It seems that when Zumo completes the authentication, owin server.user is actually installed.
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
Csrf.Enable(pipelines);
pipelines.BeforeRequest.AddItemToStartOfPipeline(FlowPrincipal);
}
private Response FlowPrincipal(NancyContext context)
{
var env = Get<IDictionary<string, object>>(context.Items, NancyMiddleware.RequestEnvironmentKey);
if (env != null)
{
var principal = Get<IPrincipal>(env, "server.User") as ClaimsPrincipal;
if (principal != null)
{
context.CurrentUser = new ClaimsPrincipalUserIdentity(principal);
}
}
return null;
}
Will issue a valid user. However, how to call login and redirection is another question.
Update3. I was able to force login using setting in Azure
, . , SignalR/Zumo, , , .