1

我有一个基本的 WPF 应用程序,客户端在其中写入数据库。我在服务器 2012 机器上使用 IIS 来托管 Web 服务。我正在尝试实现表单身份验证,并且我已经完成了所有工作(在 xaml.cs 中从客户端传递用户名和密码,该客户端对我的 ASP.NET 用户进行身份验证,该用户有效。然后我想实现 ASP.NET 角色授权不同的命令(提交请求、删除请求等)。我们应该使用的方法是“[PrincipalPermission(SecurityAction.Demand, Role = "Allowed")]"

理论上,当我尝试点击按钮时,这应该只使用客户端传递的凭据(我已经确认有效),它应该检查我传递的用户是否在角色中,如果允许,如果不允许否认。但是,无论用户是否在角色中,它仍然会显示“访问被拒绝”。

有什么想法吗?

using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Security.Permissions;
using RequestRepository;
using System.Threading;
using System.Web;

namespace RequestServiceLibrary
{
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  public class RequestService : IRequestService
  {
    private List<Request> requests = new List<Request>();
    private RequestLibraryEntities context = new RequestLibraryEntities();

    [PrincipalPermission(SecurityAction.Demand, Role = "Allowed")]
    public string SubmitRequest(Request req)
    {
        Thread.CurrentPrincipal = HttpContext.Current.User;
        if (context.Requests.Count() == 0)
            populateRequests();
        req.Id = Guid.NewGuid().ToString();
        req.TimeSubmitted = DateTime.Now;
        requests.Add(req);
        addRequest(req);
        return req.Id;
    }

    [PrincipalPermission(SecurityAction.Demand, Role = "Allowed")]
    public bool UpdateRequest(Request req)
    {
        Thread.CurrentPrincipal = HttpContext.Current.User;
        bool returnval = false;
        try
        {
            var getobject = requests.Find(x => x.Id.Equals(req.Id));
            if (getobject != null)  //checks to make sure the object isn't empty
            {
                getobject.Username = req.Username;
                getobject.Password = req.Password;
                getobject.RequestedResource = req.RequestedResource;
                getobject.TimeSubmitted = req.TimeSubmitted;
            }
            //Find the request object in the database
            var Id = Guid.Parse(req.Id);
            var rl = context.Requests.Find(Id);
            //Update that object with the values from req            
            rl.Username = req.Username;
            rl.Password = req.Password;
            rl.RequestedResource = req.RequestedResource;
            rl.TimeTransmitted = req.TimeSubmitted;
            context.SaveChanges();
            returnval = true;
            return returnval;
        }
        catch (Exception) { return returnval; }
    }
    public List<Request> GetRequests()
    {
        populateRequests();
        return requests;
    }

    [PrincipalPermission(SecurityAction.Demand, Role = "Disallowed")]
    public bool RemoveRequest(string id)
    {
        bool rval = false;
        try
        {
            Request req = requests.Find(x => x.Id.Equals(id));
            requests.Remove(req);
            rval = delRequest(req);
            return rval;
        }
        catch (Exception)
        {
            return false;
        }
    }


    private void populateRequests()
    {
        requests = new List<Request>();
        var rl = context.Requests.ToList();
        foreach (var r in rl)
        {
            requests.Add(new Request()
            {
                Id = r.Id.ToString(),
                Password = r.Password,
                RequestedResource = r.RequestedResource,
                TimeSubmitted = r.TimeTransmitted,
                Username = r.Username
            });
        }
    }

    private void addRequest(Request req)
    {
        try
        {
            var r = context.Requests.Create();
            r.Id = Guid.Parse(req.Id);
            r.Username = req.Username;
            r.Password = req.Password;
            r.RequestedResource = req.RequestedResource;
            r.TimeTransmitted = req.TimeSubmitted;
            context.Requests.Add(r);
            context.SaveChanges();
        }
        catch (DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                }
            }
        }
    }


    private bool delRequest(Request req)
    {
        Guid Id = Guid.Parse(req.Id);
        var r = context.Requests.Create();
        r.Id = Id;
        var rl = context.Requests.Find(Id);
        try
        {
            context.Requests.Remove(rl);
            context.SaveChanges();
            return true;
        }
        catch (Exception) { return false; }
    }

  }
}
4

1 回答 1

0

为了能够以PrincipalPermissionAttribute这种方式使用,您需要首先设置Thread.CurrentPrincipal为具有适当角色的 Principal(在本例中为“Allowed”)。

例如,您可以使用ClientRoleProvider来执行此操作,或者只是手动创建 Principal(可能使用您从 Web 服务检索的角色)。

于 2012-11-23T22:52:38.853 回答