C# 当所需控制器需要用户授权时,如何将数据从Web.API获取到Windows应用程序?
有很多关于通过Windows appliaction从Web.Api获取数据的示例,但我所看到的都是使用不需要用户身份验证的控件。 在我的例子中,我需要从windows应用程序中获取数据,但我需要调用的Web.Api方法是需要用户提示的控制器的方法(控制器由C# 当所需控制器需要用户授权时,如何将数据从Web.API获取到Windows应用程序?,c#,oauth-2.0,C#,Oauth 2.0,有很多关于通过Windows appliaction从Web.Api获取数据的示例,但我所看到的都是使用不需要用户身份验证的控件。 在我的例子中,我需要从windows应用程序中获取数据,但我需要调用的Web.Api方法是需要用户提示的控制器的方法(控制器由[Authorize]属性修饰) 我在这里找到了一个关于获取数据的简单示例: 但这里没有关于用户的任何信息: public void LoadData() { if (this.IsDataLoad
[Authorize]
属性修饰)
我在这里找到了一个关于获取数据的简单示例:
但这里没有关于用户的任何信息:
public void LoadData()
{
if (this.IsDataLoaded == false)
{
this.Items.Clear();
this.Items.Add(new ItemViewModel() { ID = "0", LineOne = "Please Wait...", LineTwo = "Please wait while the catalog is downloaded from the server.", LineThree = null });
WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadCatalogCompleted);
webClient.DownloadStringAsync(new Uri(apiUrl));
}
}
private void webClient_DownloadCatalogCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
this.Items.Clear();
if (e.Result != null)
{
var books = JsonConvert.DeserializeObject<BookDetails[]>(e.Result);
int id = 0;
foreach (BookDetails book in books)
{
this.Items.Add(new ItemViewModel()
{
ID = (id++).ToString(),
LineOne = book.Title,
LineTwo = book.Author,
LineThree = book.Description.Replace("\n", " ")
});
}
this.IsDataLoaded = true;
}
}
catch (Exception ex)
{
this.Items.Add(new ItemViewModel()
{
ID = "0",
LineOne = "An Error Occurred",
LineTwo = String.Format("The following exception occured: {0}", ex.Message),
LineThree = String.Format("Additional inner exception information: {0}", ex.InnerException.Message)
});
}
}
public void LoadData()
{
if(this.IsDataLoaded==false)
{
this.Items.Clear();
this.Items.Add(new ItemViewModel(){ID=“0”,LineOne=“请稍候…”,LineTwo=“从服务器下载目录时请稍候。”,LineThree=null});
WebClient WebClient=新的WebClient();
webClient.Headers[“Accept”]=“application/json”;
webClient.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(webClient\u DownloadCatalogCompleted);
DownloadStringAsync(新Uri(apiUrl));
}
}
private void webClient_DownloadCatalogCompleted(对象发送方,DownloadStringCompletedEventArgs e)
{
尝试
{
this.Items.Clear();
如果(例如,结果!=null)
{
var books=JsonConvert.DeserializeObject(如Result);
int id=0;
foreach(书籍中的书籍详细信息)
{
this.Items.Add(新的ItemViewModel()
{
ID=(ID++).ToString(),
LineOne=书名,
第二行=书。作者,
第三行=book.Description.Replace(“\n”,”)
});
}
this.IsDataLoaded=true;
}
}
捕获(例外情况除外)
{
this.Items.Add(新的ItemViewModel()
{
ID=“0”,
LineOne=“发生错误”,
LineTwo=String.Format(“发生以下异常:{0}”,例如Message),
LineThree=String.Format(“其他内部异常信息:{0}”,ex.InnerException.Message)
});
}
}
如何完成LoadData方法,以便在通过Google验证后获得结果?(Microsoft?如果我理解正确:因为您需要授权您的请求。在应用程序(WebAPI)中,创建一个名为“Attributes”的文件夹,并添加一个函数,以便在参数正确时进行检查。例如:
public class AuthWebsiteAttribute : System.Web.Http.AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var auth = actionContext.Request.Headers.Authorization;
if (auth == null)
return false;
var param = auth.Parameter;
if (String.IsNullOrEmpty(param))
return false;
// now check if the parameter is correct, for example ask your database if the // user email exists or something
return company != null;
}
}
[AuthWebsite, RoutePrefix("api/basket")]
public class SomeController : ApiController
{
/// if you are here then you are already authenticated
}
不,您的属性名为“AuthWebsite”,您可以在控制器中使用它。例如:
public class AuthWebsiteAttribute : System.Web.Http.AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var auth = actionContext.Request.Headers.Authorization;
if (auth == null)
return false;
var param = auth.Parameter;
if (String.IsNullOrEmpty(param))
return false;
// now check if the parameter is correct, for example ask your database if the // user email exists or something
return company != null;
}
}
[AuthWebsite, RoutePrefix("api/basket")]
public class SomeController : ApiController
{
/// if you are here then you are already authenticated
}
但是你如何认证呢?很简单,您需要在请求中添加一个参数:“Authorization:”。我使用的是base64编码,所以这是fiddler中的外观:
Authorization: Basic MOdzIEFyWm==
这些字符是我的密码和用户名 如果我错了,请纠正我,但您的授权规范是在IIS上为特定站点提供的基本授权策略。但我要求的是,通过谷歌、微软、Facebook等第三方身份验证提供身份验证。