locked
Request for principal permission failed RRS feed

  • Question

  • User-1305858305 posted

    Hi,

    Using Microsoft Identity and implemented Vote application from this article. 

    But I getting error in my http handler when trying to create repository. 

    Any idea?

    <%@ WebHandler Language="C#" Class="Vote" %>
    
    using System;
    using System.Web;
    using System.Web.Security;
    using System.Security.Permissions;
    using System.Web.Script.Serialization;
    using Microsoft.AspNet.Identity;
    
    [PrincipalPermission(SecurityAction.Demand, Authenticated = true)]
    public class Vote : IHttpHandler {
    
        public void ProcessRequest (HttpContext context) {
            // Make sure voteValue is -1, 0, or 1
            if (string.IsNullOrEmpty(context.Request.QueryString["articleId"]))
                throw new ArgumentException("articleId not supplied");
            if (string.IsNullOrEmpty(context.Request.QueryString["voteValue"]))
                throw new ArgumentException("voteValue not supplied");
    
            var articleId = Convert.ToInt32(context.Request.QueryString["articleId"]);
            var voteValue = Convert.ToInt32(context.Request.QueryString["voteValue"]);
    
            if (voteValue < -1 || voteValue > 1)
                throw new ArgumentException(string.Format("Invalid voteValue, {0}. Expected -1, 0, or 1.", voteValue));
    
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userId = HttpContext.Current.User.Identity.GetUserId();
                    
                // Record the vote and get the new cumulative vote score for this article
                var newVoteScore = ArticleRepository.Vote(articleId, voteValue, userId);
    
                // Return the new cumulative vote score for this article        
                var jsonSerializer = new JavaScriptSerializer();
                context.Response.ContentType = "application/json";
                context.Response.Write(jsonSerializer.Serialize(new { NewVoteScore = newVoteScore }));
            }
        }
    
        private ArticleRepository _articleRepository = null; //Error in here "Request for principal permission failed"
        public ArticleRepository ArticleRepository
        {
            get
            {
                if (_articleRepository == null)
                    _articleRepository = new ArticleRepository();
    
                return _articleRepository;
            }
        }
    
        public bool IsReusable {
            get {
                return false;
            }
        }
    
    }

    Monday, February 15, 2016 3:23 PM

Answers

  • User-1305858305 posted

    ok

    Error throwing because of user is not authenticated. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 5:25 PM