I have created a surfaceontroller and want to verify that the member is logged in when calling it.
The way I found I could do this is to add a [MemberAuthorize] attribute to the function.
Now the surface controller returns this instead
Server Error in '/' Application.
This type of page is not served.
Description: The type of page you have requested is not served because it has been explicitly forbidden. Please review the URL below and make sure that it is spelled correctly.
Requested URL: /umbraco/RenderMvc
What I wanted was to go to the loginurl presented in web.config
Is there any way I can do this?
var userTicket = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
if (userTicket != null)
{
var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
if (!currentUser.Groups.Any(x => x.Alias.Equals("admin")))
{
// Do something if the user is not an admin
Response.Redirect("~/");
}
else {
FormsAuthentication.RedirectToLoginPage();
}
}
public class MemberLoginAuthorize : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var user = System.Web.Security.Membership.GetUser();
if (user == null || user.UserName == "") {
filterContext.Result = new RedirectResult("~/");
}
else {
var member = ApplicationContext.Current.Services.MemberService.GetByUsername(user.UserName);
if (member == null)
filterContext.Result = new RedirectResult("~/"); // todo: redirect to url set in web.config form tag
}
}
}
Login page for SurfaceController
I have created a surfaceontroller and want to verify that the member is logged in when calling it.
The way I found I could do this is to add a [MemberAuthorize] attribute to the function. Now the surface controller returns this instead
What I wanted was to go to the loginurl presented in web.config Is there any way I can do this?
Hi Jan,
I think the problem is that you are using a SurfaceController (which is not authenticated). Try using UmbracoAuthorizedApiController instead.
Also see: https://our.umbraco.com/documentation/reference/routing/Authorized/#special-back-office-routes-for-user-authentication
Louis
Isn't that only for backoffice? I want to check for member, not users
Ok,
You can try something like this (found on SO)
Ended up doing my own attribute
is working on a reply...