gpt4 book ai didi

c# - wcf服务如何授权

转载 作者:行者123 更新时间:2023-11-30 18:38:05 28 4
gpt4 key购买 nike

我有一个执行某些操作的 REST Web 服务,在它做任何事情之前它授权用户,为此它调用一个方法来检查

 if(System.Web.HttpContext.Current.Session["UserId"] != null)
{
string userId = System.Web.HttpContext.Current.Session["UserId"].ToString();
}
else
{
throw exception;
}

我在我的 asp.net web 应用程序中调用此服务,如下所示

 HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Method = "POST";
req.ServicePoint.Expect100Continue = false;
req.ContentLength = 0;
req.ContentType = "application/x-www-form-urlencoded";

if (!string.IsNullOrEmpty(body))
{
req.ContentLength = body.Length;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(body);
Stream newStream = req.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}

HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
catch (WebException ex)
{
resp = (HttpWebResponse)ex.Response;

//pass on the exception
throw ex;
}
}

如何传递userId.....

我的服务和我的 web 应用程序都在同一个解决方案中,但在不同的项目中,userId 的值来自基于登录应用程序的用户的数据库。

最佳答案

关于c# - wcf服务如何授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11896434/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com