Created
July 8, 2011 01:09
-
-
Save RexMorgan/1070895 to your computer and use it in GitHub Desktop.
FubuMVC Authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
graph | |
.Actions() | |
.Where(c => c.HasInput && c.InputType().Name.StartsWith("Add") || c.InputType().Name.StartsWith("Edit")) | |
.Each(c => c.WrapWith<AuthenticationRequiredBehavior>()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthenticationConvention : IConfigurationAction | |
{ | |
public void Configure(BehaviorGraph graph) | |
{ | |
graph | |
.Actions() | |
.Where(c => c.HasAttribute<SecuredAttribute>()) | |
.Each(c => c.WrapWith<AuthenticationRequiredBehavior>()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthenticationRequiredBehavior : BasicBehavior | |
{ | |
private readonly ISecurityContext _securityContext; | |
private readonly IUrlRegistry _urlRegistry; | |
private readonly IOutputWriter _outputWriter; | |
public AuthenticationRequiredBehavior(ISecurityContext securityContext, IUrlRegistry urlRegistry, IOutputWriter outputWriter) | |
: base(PartialBehavior.Ignored) | |
{ | |
_securityContext = securityContext; | |
_urlRegistry = urlRegistry; | |
_outputWriter = outputWriter; | |
} | |
protected override DoNext performInvoke() | |
{ | |
if(_securityContext.IsAuthenticated()) | |
{ | |
return DoNext.Continue; | |
} | |
var url = _urlRegistry.UrlFor(new LoginRequestModel()); | |
_outputWriter.RedirectToUrl(url); | |
return DoNext.Stop; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DashboardEndpoint | |
{ | |
[Secured] | |
public DashboardRequestModel Get(DashboardRequestModel input) | |
{ | |
return new DashboardRequestModel(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ApplyConvention<AuthenticationConvention>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecuredAttribute : Attribute | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<system.web> | |
<authentication mode="Forms"> | |
<forms loginUrl="~/login" timeout="25" slidingExpiration="true" /> | |
</authentication> | |
</system.web> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment